Skip to content

Instantly share code, notes, and snippets.

@klamfa
Last active November 11, 2019 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klamfa/58da2a8149c5c62a6bfc5b1cb709f1b1 to your computer and use it in GitHub Desktop.
Save klamfa/58da2a8149c5c62a6bfc5b1cb709f1b1 to your computer and use it in GitHub Desktop.
Homework 5
{"Albania":"Tirana",
"Andorra":"Andorra la Vella",
"Armenia":"Yerevan",
"Austria":"Vienna",
"Azerbaijan":"Baku",
"Belarus":"Minsk",
"Belgium":"Brussels",
"Bosnia and Herzegovina":"Sarajevo",
"Bulgaria":"Sofia",
"Croatia":"Zagreb",
"Cyprus":"Nicosia",
"Czech Republic":"Prague",
"Denmark":"Copenhagen",
"Estonia":"Tallinn",
"Finland":"Helsinki",
"France":"Paris",
"Georgia":"Tbilisi",
"Germany":"Berlin",
"Greece":"Athens",
"Hungary":"Budapest",
"Iceland":"Reykjavik",
"Ireland":"Dublin",
"Italy":"Rome",
"Kazakhstan":"Astana",
"Latvia":"Riga",
"Liechtenstein":"Vaduz",
"Lithuania":"Vilnius",
"Luxembourg":"Luxembourg",
"Macedonia":"Skopje",
"Malta":"Valletta",
"Moldova":"Chisinau",
"Monaco":"Monaco",
"Montenegro":"Podgorica",
"Netherlands":"Amsterdam",
"Norway":"Oslo",
"Poland":"Warsaw",
"Portugal":"Lisbon",
"Romania":"Bucharest",
"Russia":"Moscow",
"San Marino":"San Marino",
"Serbia":"Belgrade",
"Slovakia":"Bratislava",
"Slovenia":"Ljubljana",
"Spain":"Madrid",
"Sweden":"Stockholm",
"Switzerland":"Bern",
"Turkey":"Ankara",
"Ukraine":"Kiev",
"United Kingdom":"London",
"Vatican City":"Vatican City"}
import random
import json
import datetime
def play_game(level):
secret = random.randint(1, 30)
attempts = 0
wrong_guesses = []
players_name = str(input("What is your name? Type here:"))
while True:
guess = int(input("Guess the secret number (between 1 and 30): "))
attempts += 1
if guess == secret:
with open("score_list_functions.txt", "r") as score_file:
score_list_functions = json.loads(score_file.read())
print("You've guessed it - congratulations! It's number " + str(secret))
print("Attempts needed: " + str(attempts))
current_time = datetime.datetime.now()
current_time = current_time.strftime("%d.%m.%Y, %H:%M:%S")
score_list_functions.append({"player": players_name, "attempts": attempts, "date": str(current_time),
"wrong guesses": wrong_guesses, "secret number": str(secret)})
with open("score_list_functions.txt", "w") as score_file:
score_file.write(json.dumps(score_list_functions))
break
elif guess > secret:
wrong_guesses.append(guess)
if level == "easy":
print("Your guess is not correct... try something smaller")
elif guess < secret:
wrong_guesses.append(guess)
if level == "easy":
print("Your guess is not correct... try something bigger")
def sorted_top_scores():
with open("score_list_functions.txt", "r") as score_file:
score_list_functions = json.loads(score_file.read())
sorted_scores_list = sorted(score_list_functions, key=lambda k: k['attempts'])
for score_dict in sorted_scores_list[:3]:
print(
"Name: " + score_dict["player"] + ", " + "wrong guesses: " + str(score_dict.get("wrong guesses")) + ", "
+ str(score_dict["attempts"]) + " attempts, " + "secret number was "
+ str(score_dict.get("secret number")) + ", date: " + score_dict.get("date"))
def goodbye_message():
print("Thanks for playing! Bye!")
while True:
selection = input("Would you like to A) play a new game, B) see the best scores, or C) quit?")
if selection == "A":
level = input("Choose 'easy' or 'hard' mode!")
play_game(level)
elif selection == "B":
sorted_top_scores()
else:
goodbye_message()
break
import json
import random
correct_guesses_in_a_row = 0
with open("European_countries_list.txt", "r") as information_source:
countries_and_capitals_dict = json.loads(information_source.read())
while True:
country = random.choice(list(countries_and_capitals_dict.keys()))
guessed_capital = input("What is the capital of " + country + "? ").lower()
if guessed_capital.lower() == countries_and_capitals_dict.get(country).lower():
correct_guesses_in_a_row += 1
print("That's correct! You have mad geography skillzz! Let's try another one!")
else:
print("The correct answer is " + countries_and_capitals_dict.get(country) +
"! Maybe you should practice some more! You got " + str(correct_guesses_in_a_row) +
" correct guesses in a row!")
correct_guesses_in_a_row = 0
answer = input("Do you want to play again and try to set a record? Y/N: ").lower()
if answer != "y":
print("See you next time!")
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment