4. Input and Changing Types
We know how to get information out of the program
print
And there is a similarly simple way to get information into the program
input
1my_inputted_value = input("Enter a value: ")
If you run the above code, you will see
Enter a value:Python will wait for the user to enter the value they want
Once the user enters the value, it will be stored in the
my_inputted_valuevariableThe string between the parentheses is what will be displayed to the user, but it is entirely optional
my_inputted_value = input()If you do leave it blank, nothing will be displayed to the user (this is what we want when using Kattis in lab)
Activity
Read in some value from the user.
Print out the value you entered.
What is the type of the value? How can I test this?
Hint:
type
4.1. The Type Of The Entered Value
Whenever data is input like this, Python will always treat the data as a string
If you enter
Hello world, the value ofmy_inputted_valuewould be the string"Hello world"If you enter
1, the value ofmy_inputted_valuewould be the string"1"
This may be fine in some cases, but if we want to do some math with the entered values, we do not have numbers
1first = input("Enter a number: ") # Enter 1
2second = input("Enter another number: ") # Enter 2
3print(first + second) # Results in 12 (not 3)
If you enter
1and2, you might expect3, but you will get12Since both values are strings,
+concatenates the stringsRemember, the
+operator is context sensitive
Fortunately, there is a simple way to try to change the type of the value
For example, if we want to enter the integer
1
1my_value_as_string = input("Enter a value: ")
2my_value_as_int = int(my_value_as_string)
In the above example, on the first line of code, if we enter
1as the input, the value ofmy_value_as_stringwill be the string"1"On the next line of code we are then taking the value of
my_value_as_string("1") and converting it to an integer withint(my_value_as_string)After everything, the value of
my_value_as_intwill be the integer1Writing
my_value_as_int = int(input("Enter a value: "))would achieve the same thing, but on one line of codeRemoving the middleman (
my_value_as_string)However, this does have one line of code doing multiple things
There is something nice about having one line of code do one thing
4.2. Changing Types
It is possible to use the same idea to convert the types of values, not just when reading input
For example, if I had the integer
1stored insome_integer, but wanted it to be a string, I could writestr(some_integer)intis used to convert something to an integerstris used to convert something to a stringfloatis used to convert something to a floatHowever, this assumes that the value whose type is being changed can actually be changed to that type
Python is happy to change the type of the integer
1to a float or a stringBut, if I try to change the type of the string
"Hello world"to an integer, we run into a problemint("Hello world")will cause an errorPython will say
ValueError: invalid literal for int() with base 10: 'Hello world'
Activity
Write a program to:
Ask the user to input their weight in pounds and save the value to a variable
Convert the inputted value to a float and save the result to a variable
Calculate the mass of the individual in kilograms based on the inputted weight and save the result to a variable
Print out the mass in kilograms
Hint: \(1 lbs = 0.453592 kg\)