Chapter 3, Lesson 2 Text

Chapter Three: Input and Output

Lesson Two: Getting Input from a User

 

So far, your Python programs have been a one-way street. We can print() information out to the user, but how can we build an interactive program that gets and uses input from the user? You may want the user to enter their name, answer simple questions, or provide other data. Python provides a handy input() function that allows the user to enter a piece of information.

The input() Function

You can call the input() function to ask the user a question and get a string answer. The input() function has the following form:

variableName = input(question_for_user)

This function is very simple, but very powerful! The input() parameter is a string that contains a question you want to ask the user. When the input() function runs, it will pause your program and wait for the user to type in an answer and hit "Enter" on the keyboard. The user's text response is returned, and you can store that value in a string variable or use it in some other string expression.

The following example asks the user to enter their name and stores the result in the userName variable. We then print() the userName variable out to the screen to show what the user entered. This is the first time you've run an interactive program in our live code tester, and the "Console" area will now allow you to type in a response. Try it yourself!

Try It Now

userName = input("Please enter your name: ")
print("Welcome " + userName)

When the program runs, it will print out the input question and wait for you to enter a response. Click your mouse in the "Console" area, type in your name, and press "Enter". The value that you enter is stored in the userNamevariable and then printed to the screen.

Take a close look at the prompt message we display to the user - "Please enter your name: ". Notice it has a colon and a space at the end. The input() function won't add any sort of spacing or separation automatically. So, if you don't add these yourself, the text entered by the user will appear directly after the end of the message without any spacing.

input() String Results

The input() function will always return a string value. So, if you ask the user for his or her age and get "21" as a result, that value is "21" as a string, and not 21 as a number. Try the example below. It works when you enter a numeric age for the second input() question, but it will also work when you enter something other than a number like "blue".

Try It Now

You might want the user to enter a numeric value and prompt the user to enter a number. But, the user can and might enter any sort of data. All user input is returned as a string.

Please enter your name: Maximus
Welcome Maximus
How old are you? blue
You are blue years old

Converting Numeric Input

If your program really needs numeric input, what can you do? Python provides a couple of handy functions that will turn a string value into a numeric data type - either an integer or a floating-point value. Use the int() function to convert a string to an integer or the float() function to convert a string to a floating-point value.

The example below gets a string value, converts it to an integer with int(), does some math on the resulting numeric value, and then makes use of the numeric result.

Try It Now

Try running this code three times. The first time enter a valid integer like 16. The second time enter a floating-point number like 16.5. Finally, enter a nonsense answer like "blue". What happens when you enter invalid data that cannot be stored in an integer? You should see an error message like the one below.

How old are you? 16.5
Traceback (most recent call last):
  File "code1.py", line 2, in 
    userAgeInt = int(userAgeString)                  # convert to an integer
ValueError: invalid literal for int() with base 10: '16.5'

The error means that you are trying to convert a value that is not actually an integer to an integer data type. Later in the course, we'll show you how to verify user input and avoid this kind of error. For now, we'll rely on the user to enter the correct kind of data when prompted by a program.

 

Work with Me: Mad Libs Input

 

Have you ever created a "mad lib" story, where you enter nouns, verbs and other information that gets built into a silly story? In this exercise, you are going to create your own "mad lib" program.

Use the starter code below and fill in the missing parts that are needed to get user input. Store the results in the variable names described in the comments. When you are done, our print() statement will output the mad lib story to the screen.

Try It Now

  

Console

Try running your program several times with different inputs. What kind of story can you make? Here is one example:

Please enter your name: Allstar
Please enter a noun: goldfish
Please enter a verb: jump
Please enter a number: 42
One day Allstar forgot to bring a goldfish to practice.
The coach made Allstar jump 42 laps around the field.

You can experiment on your own with additional user input and different print() output lines.

End of Lesson


Last modified: Wednesday, 8 May 2019, 8:08 AM