Unit 2 · Python mode... Activate! · Lesson 21 of 57
Converting Conundrum
Converting Conundrum
Let's go back to input() for a second, and make a program that figures out how old you are:
year = input("What year were you born? ")
age = 2026 - year
print(age)
Looks right, doesn't it? Try it.
Run it, type in a year, and see what happens.
Python exercise
Write the program in an editor next to the lesson, run it, and have it checked automatically. Create a free account to do this part.
TypeError?!
Yep. Python blew up with something like this:
TypeError: unsupported operand type(s) for -: 'int' and 'str'
In plain English: "You're asking me to subtract a string from a number. I can't do that."
But you typed a number! What gives?
Quotes mean "don't touch"
Remember way back in Silly String, when I said quotes label what Python shouldn't treat as code?
Anything inside quotes is just text. Python doesn't read it, run it, or do math with it. It just... holds it. Check this out:
pi = 3.14
print(pi)
print("pi")
3.14
pi
The first print teleports the value of pi. The second one? It's in quotes, so it's not a variable anymore. It's literally just the letters p and i.
It goes even further:
print("2 + 2")
print("print(pi)")
2 + 2
print(pi)
Python didn't do the math. It didn't even run the print! Once it's in a string, it's just text.
Question
Answer it in the lesson and it is checked as soon as you send it. Create a free account to do this part.
So what's the deal with 5 and "5"?
They're totally different things:
5is an integer. A real number you can do math with."5"is a string. It's basically a picture of a five. You can look at it, but you can't do math with it.
And here's the catch: input() always gives you back a string. Always. Even if you type 2010, you get the picture "2010", not the number 2010.
Converting (more magic!)
So how do we turn the picture of a number into an actual number? With more magic! (Seriously, there's a LOT going on under the hood here. Let's just call it magic.)
int("2010")turns the string"2010"into the integer2010str(16)turns the integer16into the string"16"float("3.14")turns the string"3.14"into the float3.14
This is called type casting (or just casting). It lets Python read "2010" the same way your brain does: as a number.
So the fix is:
year = int(input("What year were you born? "))
age = 2026 - year
print(age)
input() teleports back the string, then int() turns it into a number, and that gets stored in year. Teleportation inside teleportation!
(If it's not 2026 anymore when you're reading this, hi from the past! Just roll with it.)
It goes the other way too
What if you want to print a whole sentence with the age in it?
print("You are " + age + " years old!")
Nope! Same error, flipped around. Now you're trying to glue a number onto a string. You have to turn age back into a string first:
print("You are " + str(age) + " years old!")
Fix the age calculator!
- Ask for a birth year with
input(), and turn it into a number withint() - Work out the age with
2026 - year - Print
You are [age] years old!
If you type 2010, it should print:
You are 16 years old!
Python exercise
Write the program in an editor next to the lesson, run it, and have it checked automatically. Create a free account to do this part.
Do this lesson for real
With a free account you get the terminals, challenges and checks that go with the lessons in Intro to Python, and your progress is saved as you go. CyberAcad is a 501(c)(3) nonprofit, so there is nothing to pay.