Type Casting
Sometimes Hoppy has a number but needs to use it in a sentence. Or he has a number inside quotes but needs to do math.
We can use transformation spells (Type Casting) to change them!
The Spells
str(10)➔ turns10into"10".int("5")➔ turns"5"into5.
Example
age = 5
print("I am " + str(age)) # "I am 5"
Without str(age), the computer would complain because you cannot add text and numbers directly.
Your Mission
1
Fix the Error
The code below tries to print "Level: " + 10, which causes an error.
2
Use str()
Use str() to transform the number 10 into text so it can be joined.
Suggested SolutionClick to expandClick to collapse
level = 10
print("Level: " + str(level))Advanced TipsWant more? Click to expandClick to collapse
Be careful! int("hello") will cause an error because "hello" is not a number.
Only number-like strings can be transformed into numbers.
pymain.py
Loading...
Terminal
Terminal
Ready to run...