Here's a function from before, with a small tweak. It gets a valid integer from the user.
- def get_valid_integer(prompt, min, max):
- '''
- Get a valid integer from the user.
- Parameters
- ----------
- prompt : string
- Prompt to show the user.
- min : int
- Smallest value allowed.
- max : int
- Largest value allowed.
- Returns
- -------
- value : int
- What the user typed.
- '''
- is_input_ok = False
- while not is_input_ok:
- # Start off assuming data is OK.
- is_input_ok = True
- # Get input from the user.
- user_input = input(prompt)
- # Is the input an integer?
- try:
- value = int(user_input)
- except ValueError:
- print('Sorry, you must enter a whole number.')
- is_input_ok = False
- # Check the range.
- if is_input_ok:
- if value < min or value > max:
- print('Sorry, please enter a number between ' + str(min) + ' and ' + str(max) + '.')
- is_input_ok = False
- return value
This would be useful in many programs in your company.
Here's another function. It gets a Y or N from the user.
- def get_y_or_n(prompt):
- '''
- Get Y or N from the user.
- Parameters
- ----------
- prompt : string
- Prompt to show the user.
- Returns
- -------
- value : string
- What the user typed.
- '''
- is_input_ok = False
- while not is_input_ok:
- is_input_ok = True
- response = input(prompt)
- response = response.strip().lower()
- if response != 'y' and response != 'n':
- print('Sorry, you must enter Y or N.')
- is_input_ok = False
- return response
Again, you could use it in many programs in your company.
Could we write them once, then share them across programs? As Bob would say, "Yes, we can!"
Importing functions
You can import functions you write into your Python program. Here's a sample main program. I put it in a file called main.py
, though it could be called anything. The program calls two functions.
- about_goats()
- about_llamas()
I want it to output:
- Goat are cool!
- Llamas spit.
If I run the program now, I get undefined function errors. I need to add the functions in.
I made a file called goat.py
in the same folder as main.py
:
- def about_goats():
- print('Goat are cool!')
I also made a file called llama.py.
- def about_llamas():
- print('Llamas spit.')
OK, so we have three files:
- The file
main.py
has code calling the functionsabout_goats
andabout_llamas
. - The file
goats.py
has code for the functionabout_goats
. - The file
llamas.py
has code for the functionabout_llamas
.
Here's how I change main.py
to load in the functions from the other files.
- from goats import about_goats
- from llamas import about_llamas
- about_goats()
- about_llamas()
The first line tells Python to look for a file called goats.py
, and grab a function called about_goats
.
The second line tells Python to look for a file called llamas.py
, and grab a function called about_llamas
.
Now the program runs.
So what?
Functions like get_valid_integer
and get_y_or_n
take time to write and test. Write them once. Put them in a library file, maybe called validation-functions.py
. Then import them as needed.
Big productivity win!
You don't have to put the imported files in the same folder as the main program. You shouldn't, in fact. They should be on a shared network drive, or some such, so everyone can get to them. If you want, you can google how to import from files in other folders.
Up next
Some bonus lessons for those of you into the geekiness of it all.