Select to view content in your preferred language

capital quiz game

1132
3
Jump to solution
03-06-2022 09:48 PM
Chelsea_Trusdell
Emerging Contributor


I

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor
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!
>>> 

Have a great day!
Johannes

View solution in original post

0 Kudos
3 Replies
JohannesLindner
MVP Frequent Contributor

So, are you challenging us or do you want help?

If you want help, what do you already have?


Have a great day!
Johannes
0 Kudos
Chelsea_Trusdell
Emerging Contributor

I need help in the loop section. thanks 

0 Kudos
JohannesLindner
MVP Frequent Contributor
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!
>>> 

Have a great day!
Johannes
0 Kudos