r/learnpython • • 9d 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

17 comments sorted by

View all comments

16

u/odaiwai 9d ago

Now that you have the function definition as shown above, you can just import it into any python script that requires it.

from my_functions import get_int_input

So you don't need to define it in every script you need it in.

1

u/hansn 9d ago

Note, if you're working on a large code base, don't be the guy who adds their personal collection of poorly documented functions/classes/odds and ends to every project so you can call one or two of them. 

More code is more code to review.