I
Solved! Go to Solution.
import random
capitals = {
"Austria": "Vienna",
"Belgium": "Brussels",
"Bulgaria": "Sofia",
"Croatia": "Zagreb",
"Cyprus": "Nicosia",
"Czech Republic": "Prague",
"Denmark": "Copenhagen",
"Estonia": "Tallin",
"Finland": "Helsinki",
"France": "Paris",
"Germany": "Berlin",
"Greece": "Athens",
"Hungary": "Budapest",
"Ireland": "Dublin",
"Italy": "Rome",
"Latvia": "Riga",
"Lithuania": "Vilnius",
"Luxembourg": "Luxembourg City",
"Malta": "Valletta",
"Netherlands": "Amsterdam",
"Poland": "Warsaw",
"Portugal": "Lisbon",
"Romania": "Bucharest",
"Slovakia": "Bratislava",
"Slovenia": "Ljubljana",
"Spain": "Madrid",
"Sweden": "Stockholm",
}
exit_keywords = ["exit", "quit", "q"]
def quiz(state=None, continue_until_exit=False):
if not state:
state = random.sample(capitals.keys(), 1)[0]
capital = capitals[state]
answer = input("Input the capital of {} or type 'exit': ".format(state)).lower()
if answer in exit_keywords:
print("The right answer was {}. Thanks for playing!".format(capital))
return
answer_is_correct = answer == capital.lower()
print("That's correct!" if answer_is_correct else "Wrong, try again!")
if continue_until_exit or not answer_is_correct:
state = None if answer_is_correct else state
quiz(state, continue_until_exit)
print("Play until you get the correct answer\n")
quiz()
print("\n\nPlay until you quit manually\n")
quiz(continue_until_exit=True)
Play until you get the correct answer
Input the capital of Sweden or type 'exit': Oslo
Wrong, try again!
Input the capital of Sweden or type 'exit': Stockholm
That's correct!
Play until you quit manually
Input the capital of Hungary or type 'exit': budapest
That's correct!
Input the capital of Slovenia or type 'exit': bratislava
Wrong, try again!
Input the capital of Slovenia or type 'exit': Ljubljana
That's correct!
Input the capital of Malta or type 'exit': VALLETTA
That's correct!
Input the capital of Czech Republic or type 'exit': prague
That's correct!
Input the capital of France or type 'exit': PaRiS
That's correct!
Input the capital of Latvia or type 'exit': q
The right answer was Riga. Thanks for playing!
>>>
So, are you challenging us or do you want help?
If you want help, what do you already have?
I need help in the loop section. thanks
import random
capitals = {
"Austria": "Vienna",
"Belgium": "Brussels",
"Bulgaria": "Sofia",
"Croatia": "Zagreb",
"Cyprus": "Nicosia",
"Czech Republic": "Prague",
"Denmark": "Copenhagen",
"Estonia": "Tallin",
"Finland": "Helsinki",
"France": "Paris",
"Germany": "Berlin",
"Greece": "Athens",
"Hungary": "Budapest",
"Ireland": "Dublin",
"Italy": "Rome",
"Latvia": "Riga",
"Lithuania": "Vilnius",
"Luxembourg": "Luxembourg City",
"Malta": "Valletta",
"Netherlands": "Amsterdam",
"Poland": "Warsaw",
"Portugal": "Lisbon",
"Romania": "Bucharest",
"Slovakia": "Bratislava",
"Slovenia": "Ljubljana",
"Spain": "Madrid",
"Sweden": "Stockholm",
}
exit_keywords = ["exit", "quit", "q"]
def quiz(state=None, continue_until_exit=False):
if not state:
state = random.sample(capitals.keys(), 1)[0]
capital = capitals[state]
answer = input("Input the capital of {} or type 'exit': ".format(state)).lower()
if answer in exit_keywords:
print("The right answer was {}. Thanks for playing!".format(capital))
return
answer_is_correct = answer == capital.lower()
print("That's correct!" if answer_is_correct else "Wrong, try again!")
if continue_until_exit or not answer_is_correct:
state = None if answer_is_correct else state
quiz(state, continue_until_exit)
print("Play until you get the correct answer\n")
quiz()
print("\n\nPlay until you quit manually\n")
quiz(continue_until_exit=True)
Play until you get the correct answer
Input the capital of Sweden or type 'exit': Oslo
Wrong, try again!
Input the capital of Sweden or type 'exit': Stockholm
That's correct!
Play until you quit manually
Input the capital of Hungary or type 'exit': budapest
That's correct!
Input the capital of Slovenia or type 'exit': bratislava
Wrong, try again!
Input the capital of Slovenia or type 'exit': Ljubljana
That's correct!
Input the capital of Malta or type 'exit': VALLETTA
That's correct!
Input the capital of Czech Republic or type 'exit': prague
That's correct!
Input the capital of France or type 'exit': PaRiS
That's correct!
Input the capital of Latvia or type 'exit': q
The right answer was Riga. Thanks for playing!
>>>