[Tutor] Output never stops

Alan Gauld alan.gauld at btinternet.com
Mon Aug 4 01:08:35 CEST 2008


"David" <david at abbottdavid.com> wrote

> Thanks Alan, also your tutorial/book is a big help. I think I got it 
> :)

Close but not quite there yet.

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

Consider what happens if I enter a blank name.
You print Goodbye but then go round the loop again prompting
for another choice. You probably want the Goodbye to only
be printed if choice == '-1'?

And the test for the empty string should ptrobably be after
you ask for the input?

Also the normal way to access a dictionary value is to
use  square brackets:

person['Lary']

get() does have the advantage that you can add a default
value that is returned if the key doesn't exist. But that's
not relevant in this case.

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