I am trying to write geography quiz. I want my program to ask different question but there is something wrong with my while loop because it keeps asking the same question all over again.
places = []
i=0
myfile = open('places.csv')
for line in myfile:
row = line.strip().split(',')
if i == 0:
fields = row
i = 1
else :
place = {
'feat':row[0],
'name':row[1],
'cap':int(row[2]),
'sov':row[3],
'lat':float(row[4]),
'long':float(row[5]),
'pop':int(row[6]),
}
places.append(place)
record = random.choice(places)
qcodes = ['country','population']
def get_random(qcodes):
if qcodes == 'country':
resp = raw_input('Which country is ' + record['name'] + ' in? ');
# Valid responses
valid = {'GBR': ['GBR',
'United Kingdom',
'UK',
'Great Britain and Northern Island',
'GB'
],
'IRL': ['IRL',
'Eire',
'Republic of Ireland'
]
}
if resp in valid[record['sov']]:
answ = 'Yes, ' + record['name'] + ' is in the ' + resp
else:
answ = 'No, ' + record['name'] + ' is not in the ' + resp
print(answ)
elif qcodes == 'population':
resp = raw_input('What is the population of '+ record['name'] + ' (+/-20%)?')
try:
val = float(resp)
if ((val >= record['pop'] * 0.8) and (val<= record['pop'] * 1.2)):
answ = 'Correct, the population of ' + record['name'] + ' is ' + str(record['pop'])
else:
answ = 'Sorry, the population of' + record['name'] + ' is ' + str(record['pop'])
print(answ)
except ValueError:
answ = resp + ' is not a number'
qcode = random.choice(qcodes)
get_random(qcode)
y = 'yes'
n = 'no'
resp = raw_input('Would you like to see another question?')
while resp == 'yes':
get_random(qcode)
resp = raw_input('Would you like to see another question?')
else:
print('thanks')
This post has been edited by JackOfAllTrades: 21 October 2012 - 04:45 AM
Reason for edit:: Added code tags

New Topic/Question
Reply



MultiQuote






|