python · level 1 · unit 3 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
An fills in the gaps in your text.
Put an f before the opening quote and you get an f-string. Anything inside curly brackets {} is swapped for its value, like filling in the gaps on a birthday card.
Remember, input() always gives back text. To do maths with it, it first. int() turns text like "12" into the number 12, and str() turns a number back into text.
You can wrap one inside the other. int(input("Age? ")) asks the question and converts the answer in one go.
name = "Sam"
age = 9
print(f"Happy birthday, {name}! You are {age}.")
print(f"Next year you will be {age + 1}.")
print("Candles: " + str(age)) int() only works on text that looks like a whole number, so int("cake") gives an error.
guess first
Spot the trap. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
pet = "Rex"
print("Good dog, {pet}!")
your turn
Your turn. Look into the future.
Type your name between the quotes and your age as a number, then press run.
fix it
These dog years look very wrong. Fix them!
- Nobody is 11111111111111 in dog years! Python repeated the text 11 seven times instead of doing maths.
input()always gives back text, and text times 7 is that text repeated 7 times. Convert it to a number first.- Wrap the input in
int( )on line 1:age = int(input("How old are you? ")).
nailed it
your sentences can hold any value. :D
- Put
fbefore the quotes and values inside{}. int()converts text to a number, andstr()converts back.- Use
int(input())when you need a number from the user.
new words in your collection
- f-string
- convert
This lesson isn’t saved. A free account remembers every lesson you finish.