Question is: Write a program using a while loop that asks the user for a number, and prints a countdown from that number to zero. And include a catch if someone inputs a negative number. number = raw_input("Enter a number:") while number > 0: print number number = number - 1 Thoughts?
I think you could add some guardian condition that checks if the input is a positive number. Also, the countdoun should be to zero, while in this case I think it will stop at 1.
raw_input takes in a string, you need to convert this to a number. use int(raw_input()) You did not really put in a catch, but the way you are checking is still OK. And as ansakoy pointed out, this stops at 1. Here is a fixed version of yours and a slightly different solution with full exception handling for improper input: http://dpaste.com/hold/1275582/ Try fixing yours and get it running in IDLE or the shell, and then have a look at the way I did those as something else to think about.
E.mccormick that was really helpful. I wasn't yet familiar with int(raw_input...) that solved the problem. And thanks for sharing additional code. Part of learning a new language is seeing how others more fluent than me write and speak.
I agree. Seeing options and knowing what you did right is as important as seeing the small mistakes!
Join our real-time social learning platform and learn together with your friends!