[Tutor] Output never stops

Alan Gauld alan.gauld at btinternet.com
Sun Aug 3 10:12:35 CEST 2008


"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,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list