2 Replies - 1437 Views - Last Post: 21 October 2012 - 06:34 AM Rate Topic: -----

#1 agab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-October 12

While loop

Posted 21 October 2012 - 04:02 AM

Hi All,
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


Is This A Good Question/Topic? 0
  • +

Replies To: While loop

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: While loop

Posted 21 October 2012 - 04:45 AM

You MUST use code tags when you post code, ESPECIALLY in Python, where indentation is very important!

:code:
Was This Post Helpful? 0
  • +
  • -

#3 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: While loop

Posted 21 October 2012 - 06:34 AM

while resp == 'yes':
    get_random(qcode)
    resp = raw_input('Would you like to see another question?')



You're telling the program to ask the question "Would you like to see another question?" every time the loop runs. That's why it keeps asking.

I assume you want your get_random() function to return a random question. That means that somewhere in the definition of get_random(), you'll want to write return <something>.

Another thing. This line:

qcodes = ['country','population']


Makes a variable called qcodes, which is a list containing two strings. However, in your get_random() function, you have this line:

if qcodes == 'country':


Which asks if qcodes is equal to a the string "country". But since qcodes is a list, this will never be true.

Generally speaking, it's a bad idea to change the types of your variables. Python allows it, but it only creates trouble. If something is a number, it should keep being a number, and if something is a list, it should continue to be a list.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1