A string input function

Tags

A nice thing about functions is you can build a cut-and-paste library to save yourself time. You can take code you've already written, wrap it in a function, and reuse it.

For example, when we were talking about validating strings, there was a loop like this that keeps asking until the user enters something.

  • is_input_ok = False
  • while not is_input_ok:
  •     is_input_ok = True
  •     doggo_name = input('What is the name of your doggo? ')
  •     doggo_name = doggo_name.strip()
  •     if doggo_name == '':
  •         print('Sorry, you must enter a value.')
  •         is_input_ok = False
  •  
  • # Do something with doggo_name.

Run through that, and make sure you understand it.

In other programs, you might need the name of a person, a ferret, an atom, whatever. Say you wanted to get the name of a ferret.

  • is_input_ok = False
  • while not is_input_ok:
  •     is_input_ok = True
  •     ferret_name = input('What is the name of your ferret? ')
  •     ferret_name = ferret_name.strip()
  •     if ferret_name == '':
  •         print('Sorry, you must enter a value.')
  •         is_input_ok = False
  •  
  • # Do something with ferret_name.

It's almost the same. It seems wasteful to write the code twice with such small changes.

How about we figure out how to use the same function for ferrets, doggos, and other things?

Let's start by making the variable name more general. Maybe:

  • is_input_ok = False
  • while not is_input_ok:
  •     is_input_ok = True
  •     thing_name = input('What is the name of your thing? ')
  •     thing_name = thing_name.strip()
  •     if thing_name == '':
  •         print('Sorry, you must enter a value.')
  •         is_input_ok = False
  •  
  • # Do something with thing_name.

That works for doggos and ferrets, except for one line:

  •     thing_name = input('What is the name of your thing? ')

In future programs, we could ask about... well, anything.

Let's take the prompt, and make a variable out of it.

  •     thing_name = input(prompt)

Now here's the doggo version:

  • prompt = 'What is the name of your doggo? '
  • is_input_ok = False
  • while not is_input_ok:
  •     is_input_ok = True
  •     thing_name = input()
  •     thing_name = thing_name.strip()
  •     if thing_name == '':
  •         print('Sorry, you must enter a value.')
  •         is_input_ok = False
  •  
  • # Do something with thing_name.

Here's a version about... I dunno... comedians.

  • prompt = "Who's your fave comedian? "
  • is_input_ok = False
  • while not is_input_ok:
  •     is_input_ok = True
  •     thing_name = input()
  •     thing_name = thing_name.strip()
  •     if thing_name == '':
  •         print('Sorry, you must enter a value.')
  •         is_input_ok = False
  •  
  • # Do something with thing_name.

When you see code that's similar, take things that are different and make variables out of them. That's what we did here. The prompt was different, so we made a variable.

To make a function, wrap the code in a def and return (if you need one), and pass in the things that are different as parameters:

  • def get_required_string_input(prompt):
  •   is_input_ok = False
  •   while not is_input_ok:
  •       is_input_ok = True
  •       thing_name = input(prompt)
  •       thing_name = thing_name.strip()
  •       if thing_name == '':
  •           print('Sorry, you must enter a value.')
  •           is_input_ok = False
  •   return thing_name

We send in the prompt we want, each time we call the function. The function gives back a value.

Here's an example of how you could use it.

  • first_name = get_required_string_input("Patient's first name (required)? ")
  • last_name = get_required_string_input("Patient's last name (required)? ")
  • physician_last_name = get_required_string_input("Physician's name (required)? ")

Write the code once. Use it again and again. Reduces the cost of your MIS operations.

Another common thing is to require yes or no answers. You could write such a function once, and use it a lot.

  • def get_y_or_n(prompt):
  •   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

Call it like this:

  • extended_warranty = get_y_or_n('Do you want the extended warranty (Y/N)? ')
  • like_goats = get_y_or_n('Do you like to play with baby goats (Y/N)? ')

Remember, when you see chunks of similar code, see if you can:

  • Replace the things that are different with variables.
  • Wrap the code in a function, passing the variables in.
  • Add a return if you need one.

In the next lesson, you'll see more functions to avoid repeated code, including a numeric validation function you can copy-and-paste.