How can I create an EU quiz without 28 "if" statements
python, python-2.7
Solution
Use a dictionary to store Country->Capital, and look it up using that:
capital = {
'UK': 'London',
'Austria': 'Vienna'
}
if ans == capital[country]:
# it's correct
I would also re-work it to be based on something to pick a random number of countries (without duplicates) and use that as the main loop...
import random
number = int(raw_input())
countries = random.sample(capital, number)
for country in countries:
guess = raw_input('What is the capital of {}?'.format(country))
if guess == capital[country]:
print 'Correct!'
Problem
I am creating an EU quiz. I have gotten up to: ``` import random as r import timeit as tr import time as t print "How many questions would you like?" q = int(raw_input()) count = 0 while q > count: aus_country = r.randrange(1,29) from random import choice if aus_country == 28: country = "Austria" country_1 = ['Belgium', 'Bulgaria', 'Croatia', 'Cyprus', 'Czech Republic', 'Denmark', 'Estonia', 'Finland', 'France', 'Germany', 'Greece', 'Hungary', 'Ireland', 'Italy', 'Latvia', 'Lithuania', 'Luxembourg', 'Malta', 'Netherlands', 'Poland', 'Portugal', 'Romania', 'Slovakia', 'Slovenia', 'Spain', 'Sweden', 'United Kingdom'] country = choice(country_1) print "What is the capital of", country ans = raw_input() """I would not like to have 28 if statements here like: count = count + 1 ``` However, I would like to know if there is a better way of checking the capitals then having 28 if statements like: ``` if ans == London and country == United_Kindom: print "Correct" if ans == Vienna and country == austria: print "Correct ... else: print "Wrong" ```