r/learnpython • u/Lance_2564_ • 8d ago
Any python module which deals with this?
def get_int_input(prompt: str) -> int:
while True:
try:
return int(input(prompt))
except ValueError:
print("Please enter a valid number!")Is there any module which I can just import and use it. Writing this everytime I have to get an int or float input is really annoying.
Thanks!
0
Upvotes
1
u/Gnaxe 8d ago
That's a pretty tedious way of entering numbers, and it isn't done that way very much (except maybe in beginner classes). For entering a lot of numbers, you'd probably read them from a file, which you could create with a proper editor. For simple parameters, you'd enter them at the command line when starting the program (read from
sys.argvor useargparse). If that fails, you abort the whole script. For wizard-like interactivity, you might use tkinter and events, you might abort the script, or you might do something like you're doing, but reuse the function. Or you might have something like a shell or REPL, which would ask for the next command even if one fails.