python - How can I create an EU quiz without 28 "if" statements -


i creating eu quiz. have gotten to:

import random r import timeit tr  import time t  print "how many questions like?" q = int(raw_input())  count = 0 while q > count:         aus_country = r.randrange(1,29)         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 capital of", country         ans = raw_input()         """i not have 28 if statements here like:         count = count + 1 

however, know if there better way of checking capitals having 28 if statements like:

if ans == london , country == united_kindom:     print "correct" if ans == vienna , country == austria:     print "correct ... else:     print "wrong" 

use dictionary store country->capital, , using that:

capital = {     'uk': 'london',     'austria': 'vienna' }  if ans == capital[country]:     # it's correct 

i re-work based on pick random number of countries (without duplicates) , use main loop...

import random      number = int(raw_input()) countries = random.sample(capital, number) country in countries:     guess = raw_input('what capital of {}?'.format(country))     if guess == capital[country]:         print 'correct!' 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -