Warm-up · Activity 1 of 7
Warm-up from lesson B1.4: what does this print?
nums = [1, 2, 3]
nums.append(len(nums))
print(nums)// B1.5 · ~30 min · Beginner
After this lesson you can write a program that asks for numbers, repeats steps with a while loop, and prints a neat result: a tip calculator you build in three steps.
Lesson 5 of 5 in B1 First steps
You will be able to
Warm-up · Activity 1 of 7
nums = [1, 2, 3]
nums.append(len(nums))
print(nums)Predict · Activity 2 of 7
a, b = 0, 1
while a < 10:
print(a, end=" ")
a, b = b, a + bPractice · Activity 3 of 7
count = 3
while count ____ 0:
print(count)
count = count - 1Practice · Activity 4 of 7
x, y = 1, 2
x, y = y, x
print(x, y)Practice · Activity 5 of 7
Brain teaser · Activity 6 of 7
price = input()
print(price * 2)Apply · Activity 7 of 7
Check your work against this list
Read the worked example, then write the exercises. Your code runs in your browser or on your computer and is never uploaded.
Worked example
A lily pad covers 1 % of a pond and doubles in size every day. How many days until the whole pond is covered? The while loop repeats the doubling as long as less than 100 % is covered, printing each day on one line. Multiple assignment updates the area and the day count together. Change the starting 1.0 to 5.0 and run it again.
main.py
# How many days until a doubling lily pad covers the pond?
covered, day = 1.0, 0 # percent of the pond, days passed
while covered < 100:
print(covered, end=" ")
covered, day = covered * 2, day + 1
print()
print("Full after", day, "days")
Run it with
python main.pyOutput
1.0 2.0 4.0 8.0 16.0 32.0 64.0
Full after 7 daysTab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.
The first run downloads Python for your browser (up to 6.5 MB) and keeps it cached. Your code stays on your device.
Exercise 1 of 3
The program reads the bill with input(). Turn it into a number with float(), then print the 15 % tip and the total, both rounded to 2 decimal places. For a bill of 40: Tip: 6.0 Total: 46.0 Use input() without a prompt text, so only these two lines are printed.
Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.
The first run downloads Python for your browser (up to 6.5 MB) and keeps it cached. Your code stays on your device.
input() gives a string. float(input()) gives a number you can calculate with.
15 % of the bill is bill * 0.15; round(value, 2) keeps two decimal places.
tip = round(bill * 0.15, 2), then print("Total:", round(bill + tip, 2)).
One way to solve it. Yours can look different and still pass the checks.
bill = float(input())
tip = round(bill * 0.15, 2)
print("Tip:", tip)
print("Total:", round(bill + tip, 2))
Install Python 3.14 or newer. Save these files in one folder, open a terminal in that folder, and run the commands below.
main.py
bill = input()
# Turn bill into a number, then print the 15 % tip and the total.
print("Tip:", bill)
test_main.py
from learnrun import run_main
def tip_for(bill):
return run_main(f"{bill}\n").strip().splitlines()
def test_forty():
"""A bill of 40 gets a tip of 6.0"""
got = tip_for(40)
assert got == ["Tip: 6.0", "Total: 46.0"], f"for 40 the program printed {got!r}"
def test_hundred():
"""A bill of 100 gets a tip of 15.0"""
got = tip_for(100)
assert got == ["Tip: 15.0", "Total: 115.0"], f"for 100 the program printed {got!r}"
def test_decimals():
"""A bill of 12.4 gets a tip of 1.86"""
got = tip_for(12.4)
assert got == ["Tip: 1.86", "Total: 14.26"], f"for 12.4 the program printed {got!r}"
On macOS and Linux, type python3 wherever these commands say python, as in the first lesson.
Run the program:
python main.pyRun the checks (needs learnrun.py in the same folder):
python learnrun.py testDownload learnrun.pyExercise 2 of 3
Now the program reads three lines: the bill, the tip in percent and the number of people. Print the tip, the total and what each person pays, all rounded to 2 decimal places. For 40, 15 and 4: Tip: 6.0 Total: 46.0 Each: 11.5
Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.
The first run downloads Python for your browser (up to 6.5 MB) and keeps it cached. Your code stays on your device.
Each input() reads the next line, in order: bill, percent, people. Convert all three with float().
percent % of the bill is bill * percent / 100.
Each person pays round(total / people, 2).
One way to solve it. Yours can look different and still pass the checks.
bill = float(input())
percent = float(input())
people = float(input())
tip = round(bill * percent / 100, 2)
total = round(bill + tip, 2)
print("Tip:", tip)
print("Total:", total)
print("Each:", round(total / people, 2))
Install Python 3.14 or newer. Save these files in one folder, open a terminal in that folder, and run the commands below.
main.py
bill = float(input())
percent = input()
people = input()
tip = round(bill * 0.15, 2)
total = round(bill + tip, 2)
print("Tip:", tip)
print("Total:", total)
test_main.py
from learnrun import run_main
def split(bill, percent, people):
return run_main(f"{bill}\n{percent}\n{people}\n").strip().splitlines()
def test_fifteen_percent():
"""40 with 15 % for 4 people"""
got = split(40, 15, 4)
assert got == ["Tip: 6.0", "Total: 46.0", "Each: 11.5"], f"for 40, 15 and 4 the program printed {got!r}"
def test_other_percent():
"""100 with 20 % for 3 people"""
got = split(100, 20, 3)
assert got == ["Tip: 20.0", "Total: 120.0", "Each: 40.0"], f"for 100, 20 and 3 the program printed {got!r}"
def test_decimals():
"""12.4 with 10 % for 2 people"""
got = split(12.4, 10, 2)
assert got == ["Tip: 1.24", "Total: 13.64", "Each: 6.82"], f"for 12.4, 10 and 2 the program printed {got!r}"
On macOS and Linux, type python3 wherever these commands say python, as in the first lesson.
Run the program:
python main.pyRun the checks (needs learnrun.py in the same folder):
python learnrun.py testDownload learnrun.pyExercise 3 of 3
Read the bill, then use a while loop to print the tip for 10 %, 15 % and 20 %, one line each, rounded to 2 decimal places. For a bill of 40: 10% 4.0 15% 6.0 20% 8.0 print(percent, end="% ") writes the percent and the % sign without ending the line.
Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.
The first run downloads Python for your browser (up to 6.5 MB) and keeps it cached. Your code stays on your device.
The loop should run while percent is at most 20: while percent <= 20:
Indent both print() lines and add 5 to percent at the end of the body.
Without percent = percent + 5 inside the loop, the condition stays true and the loop never ends.
One way to solve it. Yours can look different and still pass the checks.
bill = float(input())
percent = 10
while percent <= 20:
print(percent, end="% ")
print(round(bill * percent / 100, 2))
percent = percent + 5
Install Python 3.14 or newer. Save these files in one folder, open a terminal in that folder, and run the commands below.
main.py
bill = float(input())
percent = 10
# Repeat for 10, 15 and 20 percent.
print(percent, end="% ")
print(round(bill * percent / 100, 2))
test_main.py
from learnrun import run_main
def table(bill):
return run_main(f"{bill}\n").strip().splitlines()
def test_three_lines():
"""The table has a line for 10, 15 and 20 percent"""
got = table(40)
assert len(got) == 3, f"for 40 the program printed {len(got)} line(s), expected 3: {got!r}"
def test_forty():
"""A bill of 40 gives 4.0, 6.0 and 8.0"""
got = table(40)
assert got == ["10% 4.0", "15% 6.0", "20% 8.0"], f"for 40 the program printed {got!r}"
def test_decimals():
"""A bill of 12.4 gives 1.24, 1.86 and 2.48"""
got = table(12.4)
assert got == ["10% 1.24", "15% 1.86", "20% 2.48"], f"for 12.4 the program printed {got!r}"
On macOS and Linux, type python3 wherever these commands say python, as in the first lesson.
Run the program:
python main.pyRun the checks (needs learnrun.py in the same folder):
python learnrun.py testDownload learnrun.pybill = "40" # what input() gives you: a string
print(bill * 1.15)
What Python prints
TypeError: can't multiply sequence by non-int of type 'float'Why, and the fix
input() returns a string, even when the user types digits. A string can be repeated with an int, but not multiplied by a float. Convert the input as you read it: bill = float(input()).
price = float("4,50")
What Python prints
ValueError: could not convert string to float: '4,50'Why, and the fix
Python writes decimals with a point, and float() does not accept a comma. Type 4.50 when the program asks for a number. Later you will learn to catch this error and ask again.
n = 3
while n > 0:
print(n)
What Python prints
IndentationError: expected an indented block after 'while' statement on line 2Why, and the fix
The line after while ... : must be indented, because indentation is how Python knows which lines belong to the loop. Indent the body by four spaces.
n = 3
while n > 0:
print(n)
n = n - 1
What Python prints
IndentationError: unexpected indentWhy, and the fix
Every line of one body must be indented by exactly the same amount. Here the last line has two extra spaces. Use four spaces for each level, and let the editor insert them.
Python in the browser: Pyodide 314.0.7, MPL-2.0. Licence and source
5 questions, no hints. Score 80% or more to complete the lesson.
Finish every activity above to unlock the exit ticket.