[Tutor] Programming Question

Kent Johnson kent37 at tds.net
Tue Sep 19 16:30:17 CEST 2006


cimjls wrote:
> Here is what I need to do:
> 
> Create an IF branching statement that will take the
> users input from 1-10 and return the English word for
> the number.  (1 is One, 2 is Two, etc.)  If the user
> enters a value outside of the range of 1-10, display
> an error message, and ask the user to enter a valid
> selection.
> 
> Here is what I have so far:
> 
> # Print the word for a number 1-10 entered by the user
> # If there is an incorrect value entered print an
> error message
> # and have them type in a correct value
> 
> print "\nThis program prints the English word for a
> number entered between 1 and 10."
> print "\nIf an incorrect value is entered you will get
> an error message and be asked to enter a correct
> value."
> raw_input("\n\nPlease press enter to continue.")
> 
>           
> number = int(raw_input("Please enter a number between
> 1 and 10: "))
> if number < 1:
>     print "That is an incorrect number.  Please try
> again."
>     raw_input("Please enter a number between 1 and 10:
> ")
> if number > 10:
>     print "That is an incorrect number.  Please try
> again."
>     raw_input("Please enter a number between 1 and 10:
> ")
> elif number == 1:
>     print "One"
> elif number == 2:
>     print "Two"
> elif number == 3:
>     print "Three"
> elif number == 4:
>     print "Four"
> elif number == 5:
>     print "Five"
> elif number == 6:
>     print "Six"
> elif number == 7:
>     print "Seven"
> elif number == 8:
>     print "Eight"
> elif number == 9:
>     print "Nine"
> elif number == 10:
>     print "Ten"
> 
>         
> raw_input("\n\nPress the enter key to exit.")
> 
> How do I get it to work after an incorrect number is
> entered?  I am stuck on this and would appreciate and
> help or suggestions?

What happens when you try an incorrect number? You will generally get 
better help on this list when you are very specific about what happens, 
instead of saying it doesn't work.

This looks like homework so I will just give some hints.

There is a difference between how you use the first raw_input() call and 
the ones you make when the data is bad; can you see it?

Do you know about while loops yet? If so, what happens if the user 
enters a bad number the second time? Can you fix it with a while loop?

Kent




More information about the Tutor mailing list