[Tutor] Output never stops

David david at abbottdavid.com
Sun Aug 3 17:33:15 CEST 2008


Alan Gauld wrote:
>
> "David" <david at abbottdavid.com> wrote
>>> the output never stops when I run this in a terminal
>>>
>> choice = raw_input("Enter the name Lary or Joan (-1 to quit): ")
>> while choice != '-1':           person = {'Lary': 43,'Joan': 24}
>>   if choice == 'Lary':
>>       print "Lary's age is:", person.get('Lary')
>>   elif choice == 'Joan':
>>       print "Joan's age is:", person.get('Joan')
>>   else:
>>        print 'Bad Code'
>
> You set choice outside the while loop then never change it so the 
> while test will always be true and loop forever.
> You need to copy the raw_input line into the body of the while loop to 
> reset choice.
>
> Also for good style you should move the person = {} line outside the 
> loop since you only want to set up the dictionary once, not every time 
> you execute the loop. The dictionary never changes so recreating it 
> every time is wasteful.
>
> HTH,
>
Thanks Alan, also your tutorial/book is a big help. I think I got it :)
#!/usr/bin/python

person = {'Lary':43,'Joan':24}

choice = 0
while choice != '-1':
    if choice == '':
        print "You must enter Lary or Joan to continue! (-1 to quit): "
    choice = raw_input(
            "Who's age do want to know, Lary or Joan? (-1 to quit): ")
    if choice == 'Lary':
        print "Lary's age is:", person.get('Lary')

    elif choice == 'Joan':
        print "Joan's age is:", person.get('Joan')

    else:
        print "Goodbye!"


-- 
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com



More information about the Tutor mailing list