The aim of this Python challenge is to validate a user input using a range check.
In this program, the user will be asked to enter a star rating by entering a number value between 0 and 5. This could for instance be used to rate a movie (5 Stars = Excellent , 0 Star = Disappointing).
The program will check that the end-user has entered a number between 0 and 5 and if not, it will display an error message and repeat the question until a valid rating between 0 and 5 is entered.
Here is the flowchart for our algorithm:
Python Code
You can now complete the Python code to implement this algorithm.
This algorithm is a good example of:
Testing
Now that your code is complete, it is important to test it to see if it is working as expected.
When testing a program, you can use different types of test and test data:
- Valid Tests: To check that the program is working fine with acceptable data.
- Extreme Tests: To test how your program works when you enter data that is at the limit (lower boundaries, upper boundaries) of what is considered acceptable data.
- Erroneous Tests: To try to break the pogram, enter data that is not acceptable, that should be rejected and/or generate an error message.
Test # | Type of Test | Input Values | Expected Output | Actual Output |
#1 | Valid | Star Rating: 2 | Thank You | |
#2 | Valid Extreme | Star Rating: 0 | Thank You | |
#3 | Valid Extreme | Star Rating: 5 | Thank You | |
#4 | Erroneous | Star Rating: 9 | Invalid Star Rating – Try Again Enter a star rating between 0 and 5: |
|
#5 | Erroneous | Star Rating: -2 | Invalid Star Rating – Try Again Enter a star rating between 0 and 5: |
|
#6 | Erroneous | Star Rating: xyz | Invalid Star Rating – Try Again Enter a star rating between 0 and 5: |
Extension Task
Did you all your test pass? If not, this means your code could be further improved.
Check this blog post to see if it could help you improve your code further and ensure all tests pass.
Adding validation routines when capturing user inputs is always a good approach to improve your programs and make them more robust.
You can investigate other forms of validation routines on this page.
Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area