🐸

The Transformation

Python Basicspython-basics-07-the-transformation
Reward: 60 XP
|

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) ➔ turns 10 into "10".

  • int("5") ➔ turns "5" into 5.

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 Solution
Click to expand
level = 10
print("Level: " + str(level))
Advanced Tips
Want more? Click to expand

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...