Hi Glen, and welcome! My responses below. On Thu, May 15, 2014 at 09:58:07PM -0400, Glen Chan wrote:
> Hello, I am student trying to fugure out why when I enter any number > it says error. It's only suppose to do that if it's out the 1-10 > range. Please help. Thank you. You've made an mistake in your logic below. You print an error for *every* number: > 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: ') This part is okay. You check that if number is out of range, and if it is, it prints a message. Check for yourself: "Suppose I choose 5. Is 5 less than 1? No. Is it larger than 10? No. So the while loop immediately ends, and I move on." So at this stage, you have now successfully asked the user for a number between 1 and 10. But here you make the mistake: > number = input('Enter a number between 1 and 10: ') Hmmm. You've already asked the user for a number. But then you ignore it, and ask for another one. But even that's not really the mistake: > while number > 1 or number < 10: > print 'Error' > number = input('Enter a number between 1 and 10: ') And this is where you get the logic backwards. This while loop runs forever, or until you manually cancel it. Check for yourself: "Suppose I choose 5. Is 5 larger than 1? Yes. So the while loop continues, and 'Error' is printed. Suppose I choose 0. Is 0 larger than 1? No. Is 0 less than 10? Yes. So again, the while loop continues, and 'Error' is printed. Are there *any* numbers *smaller* than 1 AND *larger* than 10 at the same time? No, of course not. So the while loop condition is always true, and the while loop will always run no matter what number I choose." This second while loop has the logic backwards. You should compare this one, the faulty one, with the first one, the correct one. Can you see the difference? A couple of other comments about your code: * In a few places, you use "input", but in other places, you use "raw_input". You should never use "input". It was a mistake, and has been removed from newer versions of Python. Instead, you should write int(raw_input("Enter a number...")). * You check that number is in range like this: number < 1 or number > 10 There's an easier way: 1 <= number <= 10 which is not only less typing, but makes it more obvious what you are trying to do. You want number to be in the range 1 through 10 inclusive. That makes it harder to screw up the logic: 1 >= number >= 10 # Wrong! "What do you mean, 1 is BIGGER than number, which is bigger than 10? That's impossible!" -- Steven _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor