The Input Function: My - Age ( (My - Age) )
The Input Function: My - Age ( (My - Age) )
In this notebook we look at how user input can be obtained and stored in variables. The input() function returns values
whose data type is string. When you wish to accept numeric values from the user, the input function must be used in
combination with the int() or float() function
In [1]:
my_age = input() #Note the lack of a space between the prompt and the user's response.
print(type(my_age))
# What is the type of my_age?
12
<class 'str'>
In [2]:
my_age = input('Enter your age:') #Note the lack of a space between the prompt and the user's resp
onse.
print(type(my_age))
# What is the type of my_age?
In [3]:
my_str_age = input('Enter your age: ')
my_age = int(my_str_age) # Now what is the type of my_age
print(type(my_age))
In [1]:
price = float(input('Enter the price of the book: '))
print(price)
# What is the type of price? What happens if you use int() instead of float()?