I think the abstraction is wrong. input_int should return an int, not int or False. Returning a value or a special flag value is how a C programmer would write the function, back in the 70's. The abstraction is described as "A function that returns an integer input", not "A function that returns an integer input, or False if the input is not a valid integer string". A better abstraction lets the calling code assume the return value is correct. No more while loops and checking, just getting down and doing what you want to do:
def input_int(prompt="Enter integer: "):
"""Return integer input"""
while True:
value = input(prompt)
try:
return int(value)
except ValueError:
print(value, "is not an integer")
print("The product is", input_int() * input_int())
Output:
Enter integer: 5
Enter integer: 5
The product is 25
That little function can handle a lot of input cases, but maybe not yours. Maybe you want a version with an escape word. The function returns an integer input but provides a way for the user to not comply. This example returns an int unless the user enters a special escape word. If that happens, the function raises a ValueError. The caller is still written in a way that assumes the user will provide valid input, but it wraps that code in a handler that catches the exit.
def input_int(prompt="Enter integer: ", exit_str=None):
"""Returns an integer input.
prompt : Optional prompt printed to aid input
exit_str : Escape word that terminates input. Raises ValueError when encountered.
while True:
value = input(prompt)
if value == exit_str:
raise ValueError(exit_str)
try:
return int(value)
except ValueError:
print(value, "is not an integer")
try:
print("The product is", input_int(exit_str="quit") * input_int(exit_str="quit"))
except ValueError as msg:
print("Got", msg)
Output:
Enter integer: 5
Enter integer: a
a is not an integer
Enter integer: exit
exit is not an integer
Enter integer: quit
Got quit
The same function can be used to enter numbers until a blank input.
try:
values = []
while True:
values.append(input_int("Enter integer or blank ", exit_str=''))
except ValueError:
print(values)
Output:
Enter integer or blank 1
Enter integer or blank 2
Enter integer or blank 3
Enter integer or blank 4
Enter integer or blank 5
Enter integer or blank
[1, 2, 3, 4, 5]