#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?

Line 60 does not make sense as it is not in a if statement. if of line 39 ends of course line 53 as indentation is outdented at line 54 to main program level.

lines 42 to 53 is a directory

Does line 60 need indenting to be in the IF?

The last bit is very confusing, can't work out what you are saying.

#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]),
            'sov': 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['sov']]:
    answ = 'Yes, ' + record ['name'] +' is in the ' + resp
else:
    answ = 'No, ' + record ['name'] +' is not in the ' + resp
# Test Print
print(answ)
if 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'

got following message after some tweaking of the code

Traceback (most recent call last):
  File "F:\GS2655\Assignment_A\gq.py", line 54, in <module>
    if resp in valid[record['sov']]:
NameError: name 'resp' is not defined

The if resp ... part should be indented.

Between an if and a corresponding elif, all lines must be indented.

keeps saying unexpected indent when I indent.

managed to fix it, it was indents, had to play about with them. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.