Getting Input and Converting Types

CS 65: Introduction to Computer Science I

User Input

To receive input from the user, use the built-in input() function.

In [1]:
name = input("Enter your name ")
print("Hello",name) 
Enter your name Eric
Hello Eric

Think about the name variable here. What is its type? How would we check?

In [2]:
type(name)
Out[2]:
str

User input example: Fahrenheit to Celsius converter

The formula for converting Fahrenheit temperatures to Celsius is $$C = (F - 32)*\frac{5}{9}$$

We could make this an interactive program with code like this:

In [3]:
fahrenheit_temp = input("Enter the temperature in Fahrenheit ")
celsius_temp = (fahrenheit_temp-32)*(5/9)
print("That's",celsius_temp,"in Celsius") 
Enter the temperature in Fahrenheit 32
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-fc29a04c7392> in <module>
      1 fahrenheit_temp = input("Enter the temperature in Fahrenheit ")
----> 2 celsius_temp = (fahrenheit_temp-32)*(5/9)
      3 print("That's",celsius_temp,"in Celsius")

TypeError: unsupported operand type(s) for -: 'str' and 'int'

Read the error message! It says we can't subract an int from a string. Even though we typed a number, it treated fahrenheit_temp as a string.

Unfortunately, that's just the way input() works.

Fortunately, there's another built-in function we can use to convert something to an integer: int()

In [4]:
val = "32"
print( type(val) )
print( type(int(val)) )
<class 'str'>
<class 'int'>
In [6]:
fahrenheit_temp_str = input("Enter the temperature in Fahrenheit ")
fahrenheit_temp_int = int(fahrenheit_temp_str)
celsius_temp = (fahrenheit_temp_int-32)*(5/9)
print("That's",celsius_temp,"in Celsius")
Enter the temperature in Fahrenheit 100
That's 37.77777777777778 in Celsius

Or, you can do it all in one step like this

In [9]:
fahrenheit_temp = int( input("Enter the temperature in Fahrenheit ") )
celsius_temp = (fahrenheit_temp-32)*(5/9)
print("That's",celsius_temp,"in Celsius")
Enter the temperature in Fahrenheit 72
That's 22.22222222222222 in Celsius

What would happen if the user typed a floating-point value like 72.5?

Other type conversion functions

Convert to a floating-point type with float()

Convert to a string with str()

In [10]:
fahrenheit_temp = float( input("Enter the temperature in Fahrenheit ") )
celsius_temp = (fahrenheit_temp-32)*(5/9)
print("That's",celsius_temp,"in Celsius")
Enter the temperature in Fahrenheit 72.5
That's 22.5 in Celsius
In [15]:
target_number = 42
user_input = int( input("Enter a number greater than ",target_number) ) #doesn't work
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-120d707f2722> in <module>
      1 target_number = 42
----> 2 user_input = int( input("Enter a number greater than ",target_number) ) #doesn't work

TypeError: raw_input() takes from 1 to 2 positional arguments but 3 were given
In [14]:
#above code corrected
target_number = 42
user_input = int( input("Enter a number greater than "+str(target_number)+" ") )
Enter a number greater than 42 43