[Tutor] Help with Python
Alan Gauld
alan.gauld at btinternet.com
Fri May 16 10:07:07 CEST 2014
On 16/05/14 02:58, Glen Chan wrote:
> Hello, I am student trying to fugure out why when I enter any number it
> says error.
Because that's what you programmed it to do.
Almost. If you enter 1 or 10 you won't get an error.
Look at your logic:
> number = input('Enter a number between 1 and 10: ')
> while number < 1 or number > 10:
> print 'Please enter a number between 1 and 10'
> number = input('Enter a number between 1 and 10: ')
>
> number = input('Enter a number between 1 and 10: ')
> while number > 1 or number < 10:
> print 'Error'
> number = input('Enter a number between 1 and 10: ')
If the first number you enter is say 7.
It skips the first while loop and asks for another number.
If you enter 7 again
It then goes into the second while loop and reports an error.
The only numbers which don't fall into one of the two loops
are 1 and 10.
BTW It's bad practice to use input() in Python v2 becauise it has
security issues. Instead use raw_input() and convert to an
int() or float() explicitly.
> while not (endProgram == 'yes' or endProgram == 'no'):
> print 'Please enter a yes or no'
There is a bug here too since you don't provide a way
for the user to enter a new number. It will loop forever.
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list