Start Your Journey with Linux Command Line
| Reading ints safely |
Your task is to write a function able to input integer values and to check if they are within a specified range.
| LAB Sandbox |
The function should:
accept three arguments: a prompt, a low acceptable limit, and a high acceptable limit;
if the user enters a string that is not an integer value, the function should emit the message Error: wrong input, and ask the user to input the value again;
if the user enters a number which falls outside the specified range, the function should emit the message Error: the value is not within permitted range (min..max) and ask the user to input the value again;
if the input value is valid, return it as a result.
Test your code carefully.
This is how the function should react to the user's input:
Enter a number from -10 to 10: 100
Error: the value is not within permitted range (-10..10)
Enter a number from -10 to 10: asd
Error: wrong input
Enter number from -10 to 10: 1
The number is: 1
def read_int(prompt, min, max):
ok = False
while not ok: #---> True
try:
value = int(input(prompt))
ok = True
except ValueError:
print("Error: Wrong Input")
if ok:
ok = value >= min and value <= max
if not ok:
print("Error: The value is not within range(" + str(min) + ".." + str(max) + ")")
return value
v = read_int("Enter a number from -10 to 10: ", -10, 10)
print("The number is:", v)
LAB 2.8.1.4 "Reading ints safely" asks you to write a function named read_int that accepts three arguments: a prompt string, a low limit, and a high limit. The function must repeatedly ask the user for input until a valid integer within the specified range is supplied. This lab ties together two skills: defining your own functions and using exceptions to create a safe input environment that never crashes on bad data.
The function works by looping until a valid value is found. A boolean flag, here named ok, starts as False. Inside a while not ok loop, the program attempts to convert the user's input to an integer with int(input(prompt)). If that conversion succeeds, the flag is set to True; if it fails with a ValueError, the except block prints "Error: Wrong Input" and the loop continues so the user can try again.
Once a number has been successfully converted, the function must still check that it lies within the allowed bounds. The ok flag is then updated with ok = value >= min and value <= max. If the value falls outside the range, this expression becomes False, and the code prints a message like "Error: The value is not within range(min..max)" before looping again. Only when the value is both an integer and within range does the flag remain True, allowing the function to return the value.
This combination of a try/except for the type check and a conditional test for the range check makes the function robust to every kind of invalid input. Whether the user types letters, symbols, or a number that is simply too large, the program keeps prompting rather than terminating. The pattern of a validation loop driven by a boolean flag is reusable anywhere you need trustworthy numeric input.
read_int function takes a prompt, a low limit, and a high limit as arguments.while not ok loop drives the repeated prompting until valid input arrives.int(input(prompt)) inside a try block catches a ValueError for non-integer input.value >= min and value <= max rejects numbers outside the allowed bounds.Calling int() on a string that is not a valid integer raises a ValueError. Catching that exception lets the function print a message and ask again instead of crashing.
The flag keeps the loop condition simple and lets the same outer loop handle both conversion errors and range errors. Each error just resets the flag to False so the loop repeats.
After a successful conversion, the expression value >= min and value <= max determines whether the number is within bounds. If not, the program prints a range error and loops again.
Once a value is both a valid integer and within the specified range, the function returns that number so the caller can use it, for example by printing "The number is:" followed by the value.
Comments
Post a Comment
Your opinion matters, your voice makes us proud and happy. Your words are our motivation.