code: TU09 points: 12
Sometimes you want to try something without the program crashing if it does not work out. A good example is if you ask somebody to enter a number and they decide to enter letters or decimals instead. You could just let Python throw a wobbly, but its better to anticipate it!
The commands to notice are:
try: - This says that this might not work
except ValueError: This checks that it is a valid number. If somebody enters something that is not a whole number, it will give you the opportunity to say try again...
Exercise 1: Use the code above, but count the number of times that somebody enters an error! Don't let them get away with it.
try: - This says that this might not work
except ValueError: This checks that it is a valid number. If somebody enters something that is not a whole number, it will give you the opportunity to say try again...
Exercise 1: Use the code above, but count the number of times that somebody enters an error! Don't let them get away with it.