Chapter 8: Input and Output
5 Find the Bug exercises from Chapter 8 of Everyday Programming. Every program below is short, does something recognizable, and hides exactly one bug — syntax, runtime, or logical. Read it, predict what it does, then run it and find out.
Worked solutions for this chapter: Chapter 8 solutions. Every snippet is also available as a .py file — see Exercises & Solutions.
Exercises 8.1.1–8.1.5
Exercise 8.1.1 — Doubling a recipe
This program asks how many cookies a recipe makes, then prints double that amount.
# Assume the user types: 12
cookies = input("How many cookies does the recipe make? ")
print(f"Doubled, that is {cookies * 2} cookies.")
Exercise 8.1.2 — Average of two test scores
Assuming the user types 80 then 90, this program should print Your average is 85.0..
# Assume the user types: 80 then 90
score1 = float(input("First test score? "))
score2 = float(input("Second test score? "))
average = score1 + score2 / 2
print(f"Your average is {average}.")
Exercise 8.1.3 — Printing a shopping list
This program should print three fruits on one line, separated by commas, like apple, banana, cherry.
fruits = ["apple", "banana", "cherry"]
print(fruits[0], fruits[1], fruits[2], sep=" ")
Exercise 8.1.4 — Saving a temperature reading
This program writes one weather reading to a file and then prints it back.
with open("reading.txt", "w", encoding="utf-8") as f:
f.write("Tokyo,36.5\n")
with open("reading.txt", "w", encoding="utf-8") as f:
contents = f.read()
print(contents)
Exercise 8.1.5 — Greeting by name
This program asks for a name and prints a greeting.
# Assume the user types: Ava
name = input("What is your name? ")
print(f"Hello, {name}! Welcome aboard.)