Input Validation
You have a working CO2 calculator, but what happens if the user enters a number that the program isn’t expecting, or something that isn’t a number at all? Did you see an error called “IndexError”?
In the next step we’ll validate the user input so that if they enter something incorrect, it doesn’t crash the whole program.
- The first step in validation is to tell the user what is expected.
- Let’s tell our user what is expected of them when you ask them for their answer.
- The ‘format’ command might be a little confusing, but for now all you need to know is that it replaces ‘{}’ with the number stored in ‘i’.
- Run your program. You might notice that the range given is one larger than the number of options. Oh no! We can fix this substituting “i-1” instead of “i” in format.
Run your program again. That’s better!
- Now we’re telling the user what to enter, they should get it right, but just because they should get it right, it doesn’t mean they will! Everyone makes mistakes, and some people will deliberately try and break programs (that’s one of the ways that hackers can break into computers). We still need to check that the input is valid.
- Let’s start by checking that a number is actually entered before we tell python to convert it into a number.
- (Note that we’ve indented line 14 to the next level to tell Python that it’s inside the if
- statement)
If the user enters something that isn’t a digit, we should tell them that they need to enter a number.
- Next, we need to validate the number, to ensure that it’s in the right range, only then will we add the number to the total. Add the ‘if’ below, and indent the existing ‘total +=’ line.
- Run your program.
- What happens if the user enters something wrong? It doesn’t break the program anymore, but it just goes on to ask the next question, and so won’t give an accurate result. We want to ask the user the question again and again until they give a valid response.
- To achieve this, we’ll use a while loop. This will keep looping through until the condition is
- satisfied (in this case, we get a valid response). Start by initialising a Boolean variable.
- We will assume a response is not valid until we’ve checked everything required to tell us that it is.
- Our while loop will run all of our validations.
- (Note that we’ve indented everything up to line 21 to another level. You can do this by highlighting all of the code between lines 14-21 that you want to indent and pressing the TAB key.)
- If you run your program now, you will be stuck in an endless loop, because we never set valid to True. Let’s do that if our number is between the correct range.
- Note that we’ve moved line 23 out of the if statement and the while loop by decreasing the indent by two levels. We did this by using the backspace at the beginning of the line.
- If the user enters a number outside of the correct range, we should tell them what they’ve done wrong
- Run your program. It should now be working and correctly validating user responses.