Re: [Tutor] Output never stops
David wrote: [snip] #!/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! Consider leveraging the dictionary (and some otherPythonic refinements). Separate the logic from the data. Now you can add more names and ages without changing the logic. #!/usr/bin/python person = {'Lary':43, 'Joan':24, 'Bob':68} keys = person.keys() names = ', '.join(keys[:-1]) + ' or ' + keys[-1] while True: choice = raw_input(Who's age do want to know, %s? (-1 to quit): % names) age = person.get(choice, None) if age is not None: print choice + 's age is:, age elif choice == -1: print Goodbye! break else: print Invalid choice + choice -- Bob Gailer 919-636-4239 Chapel Hill, NC ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Output never stops
David [EMAIL PROTECTED] 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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Output never stops
Alan Gauld wrote: David [EMAIL PROTECTED] 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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Output never stops
David [EMAIL PROTECTED] 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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Output never stops
Very new to python and never programed before. Can not figure out why the output never stops when I run this in a terminal #!/usr/bin/python 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' -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Output never stops
David wrote: Very new to python and never programed before. Can not figure out why the output never stops when I run this in a terminal #!/usr/bin/python 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' should have been like this; #!/usr/bin/python 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' -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Output never stops
Hi David, Simple explanation - you should use if instead of while. A while statement executes whatever is in that block again and again until the condition becomes false. So your code, the way you have it, first checks if the user's input is not -1. If you have typed Lary, it goes on to print Lary, hits the end of the while block, and starts back at the top, since choice still is not -1. If you change while to if, the statement will only be executed once. Here's a scenario you might want to use a while loop (which might be what you were trying to do): suppose you want to keep on asking for input if the input is not a quit code, or valid input. To do this, you could just put the choice = raw_input line inside the while block, and perhaps make sure to initialize choice to '' (an empty string or something). Vivek On Aug 2, 2008, at 10:21 PM, David wrote: David wrote: Very new to python and never programed before. Can not figure out why the output never stops when I run this in a terminal #!/usr/bin/python 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' should have been like this; #!/usr/bin/python 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' -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor