Prompting the User with input

Most programs need input of some kind. Programs can take input from the keyboard using the input function. When Python encounters the input function a text box will appear below the code cell. The program will halt until you provide a value and hit the Enter key.

Watch out! If a code cell is waiting on input no other code cells will run. If your notebook is lost in an input somewhere you can try to stop it using the “Stop” or “Resart” buttons in the menu bar.

The input() function returns what the user typed to your program. We will talk a lot about return values later in the semester. When a function returns a value you have to catch that value into a variable. That’s done using the assignment operator you learned in lesson 5. In this example the input function places what the user types into the variable answer.

answer = input("What is 1 + 1? ")
print("You asnwered:", answer)

Enter the code into the next cell and try it. Don’t forget to type an answer:

[ ]:

Anything you type will be stored in the ``answer`` variable. The name of the variable doesn’t matter, you can call it anything you like. The prompt is the string that appears to the left of the text box. The prompt tells the user what to type. In most programs you can’t just see the code so the prompt is an important way for a user to know what’s going on. The prompt can be anyting. For example:

nonsense = input("Blah blah blah")

The prompt can be nothing at all:

guess = input()

It’s a bad idea to neglect the prompt because the user won’t know what you’re asking for and the program will be stopped waiting for them to type something in.

Conversions

The input function returns a string (even if the user types a number). Try this program:

num = input("Enter a number between 1 and 100: ")
print(f"You entered {num}")
print(f"The type of num is {type(num)}")
[ ]:

Notice how even if you typed a number the ``type`` of the numer is ``str``?

If you want to do math on the result you have to convert the string into a number. In Python you can turn a string into a number using int() for integer and float() for a number with a decimal pace. Be careful, an error will happen if you try to convert something that doesn’t make sense. Here’s an example of how to convert a string into numbers:

a = 10
print("a is", a)
print("As an integer:", int(a))
print("As a float:", float(a))

Try the code:

[ ]:

The conversion functions return the converted value. So you can make new variables from them. For example:

a = 10
a_int = int(a)
a_float = float(a)
print(a, a_int, a_float)

Converting Input

Below is a simple program that adds two numbers that has a problem:

a = input("Enter an integer: ")
b = input("Enter another integer: ")
print("The sum of the numbers is:", a + b)

Enter the program in the next cell:

[ ]:

What’s the problem? Remember that + works on strings as well as numbers. What happened is concatenation, not addition. To fix the program you have to convert the values in a and b into integers. You can do that inline like this example:

a = int(input("Enter an integer: "))
b = int(input("Enter another integer: "))
print("The sum of the numbers is:", a + b)

Try that:

[ ]:

Much better!

If you want to be able to take any number you should convert the input to float:

a = float(input("Enter a number: "))
b = float(input("Enter another number: "))
print("The sum of the numbers is:", a + b)
[ ]: