Skip to main content

Section 3.13 Input

The program in section SectionΒ 3.6 for conversion from seconds, to hours, minutes and seconds demonstrated how to use the print function to produce output. The program in the previous section works fine but is very limited in that it only works with one value for total_secs. What if we wanted to rewrite the program so that it was more general. One thing we could do is allow the user to enter any value they wish for the number of seconds. The program could then print the proper result for that starting value.
In order to do this, we need a way to get input from the user. Luckily, in Python there is a built-in function to accomplish this task. As you might expect, it is called input .
n = input("Please enter your name: ")
The input function allows the user to provide a prompt string. When the function is evaluated, the prompt is shown. The user of the program can enter the name 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. Make sure you run this example a number of times and try some different names in the input box that appears.
It is very important to note that the input function always returns a string value. Even if you asked the user to enter their age, you would get back a string like "17".
For example, enter the characters17 or any integer as the input in the code block below and see its type
As shown above, even though an integer was entered, the value stored in m is a string. Because of this, the value cannot be used directly in numeric calculations.
To use the input as a number, the string must be converted to a numeric type. Python provides built-in conversion functions such as int and float for this purpose.
m = input("Please enter your age: ")
age = int(m)
print(age)
print(type(age))
After conversion, the value stored in age is an integer and can be used in calculations.
print(age + 1)
If the input may contain a decimal value, the float function can be used instead.
height = float(input("Please enter your height in meters: "))
print(height * 2)
As the programmer, it is your responsibility to choose the appropriate type conversion based on how the input value will be used. Understanding when and how to convert input values is essential for writing correct programs and avoiding type-related errors.
Here is another example of 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.
Check your understanding

Checkpoint 3.13.1.

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.

Checkpoint 3.13.2.

Checkpoint 3.13.3.

You have attempted of activities on this page.