#import libraries
import random
#
#create empty list
places = []
i = 0
# open file handle
myfile = open ('places.csv')
# Subsquentially read lines
for line in myfile:
row = line.strip().split(',')
# first line of test if
if i == 0:
fields = row
i = 1
else :
# process record
# creates a directory
place = {
'feat': row[0],
'name': row[1],
'cap': int(row[2]),
'scv': row[3],
'lat': float(row[4]),
'long': float(row[5]),
'pop': int(row[6])
}
# appends places to place
places.append(place)
record = places[random.randrange(0,len(places))]
# prints record
# print(record)
#
#
# Questions
qcodes = ['country', 'population']
qcode = qcodes[random.randrange(0,len(qcodes))]
# If statement - asks questions
if qcode == 'country':
resp = raw_input ('Which country is ' + record['name'] + ' in? ')
# Valid responses
valid = {'GBR': ['GBR',
'United Kingdom',
'UK',
'Great Britian and Northern Ireland',
'GB'
],
'IRL': ['IRL',
'Eire',
'Republic of Ireland'
'Ireland'
]
}
if resp in valid[record['scv']]:
answ = 'Yes, ' + record ['name'] +' is in the ' + resp
else:
answ = 'No, ' + record ['name'] +' is not in the ' + resp
# Test Print
print(answ)
elif qcode == '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 + 'it is not a number'
Trying to randomly select either question about country or population. The country question works, but the population one doesn't if I just put an if instead of an elif, when I put that I get invalid syntax. Can anyone help?