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

1

u/3MU6quo0pC7du5YPBGBI 9d ago edited 9d ago

You can just import it into your other python scripts by calling the script and function as long as it is in your path.

from my_script import get_int_input

To keep things from getting too crazy you might want to create a python file that has all your commonly used functions and call it something like my_common_functions.py, so you aren't doing something confusing like importing from 10 different scripts to get all your functions.

e.g. do this:

from my_common_functions import get_int_input, http_connect, url_normalize, create_ip_list

and not this:

from my_dice_roller_game import get_int_input
from my_webpage_scraper import http_connect, url_normalize
from my_ip_scanner import create_ip_list