Skip to main content

Section 2.15 Input

Our programs get more interesting if they don’t do exactly the same thing every time they run. One way to make them more interesting is to get input from the user. Luckily, in Python there is a built-in function to accomplish this task. It is called input.
n = input("Please enter your name: ")
The input function allows the programmer to provide a prompt string. In the example above, it is β€œPlease enter your name:”. When the function is evaluated, the prompt is shown (in the browser, look for a popup window). The user of the program can type some text and press return. When this happens the text that has been entered is returned from the input function, and in this case assigned to the variable n. Run this example a few times and try some different names in the input box that appears.
It is very important to note that the input function returns a string value. Even if you asked the user to enter their age, you would get back a string like "17". It would be your job, as the programmer, to convert that string into an int or a float, using the int or float converter functions we saw earlier.

Note 2.15.1.

We often use the word β€œinput” (or, synonymously, argument) to refer to the values that are passed to any function. Do not confuse that with the input function, which asks the end user of a program to type in a value. Like any function, input itself takes an input argument and produces an output. The input is a character string that is displayed as a prompt to the user. The output is whatever character string the user types.
Here is a program that turns a number of seconds into more human readable counts of hours, minutes, and seconds. A call to input() allows the user to enter the number of seconds. Then we convert that string to an integer. From there we use the division and modulus operators to compute the results.
The variable str_seconds will refer to the string that is entered by the user. As we said above, even though this string may be 7684, it is still a string and not a number. To convert it to an integer, we use the int function. The result is referred to by total_secs. Now, each time you run the program, you can enter a new value for the number of seconds to be converted.
Here is a turtle example that asks the user for input on where the turtle should go:
The input functions on lines 5 and 6 ask the user to enter coordinates and saves the resulting strings to the user_x and user_y variables. The statements of lines 8 and 9 convert the strings to integers. On line 11, the turtle draws a line from the center of the window to wherever the user specified.
Check your understanding

Checkpoint 2.15.2.

    What is printed when the following statements execute?
    n = input("Please enter your age: ")
    # user types in 18
    print(type(n))
    
  • <class ’str’>
  • All input from users is read in as a string.
  • <class ’int’>
  • Even though the user typed in an integer, it does not come into the program as an integer.
  • <class 18>
  • 18 is the value of what the user typed, not the type of the data.
  • 18
  • 18 is the value of what the user typed, not the type of the data.
You have attempted of activities on this page.