A7. Write a program that stores your favourite food in a variable called food and the number of times you eat it per week in a variable called times. Then print a sentence like:
Output:
I eat pizza 2 times a week
Solution
food = "pizza"
times = 2
print("I eat", food, times, "times a week")
A8. Write a program that asks the user for the length and width of a rectangle (whole numbers) and prints its area.
Use input() and convert with int().
Solution
length = int(input("Enter the length: "))
width = int(input("Enter the width: "))
area = length * width
print("The area is", area)
Section B: Conditionals
B1. What will each expression give, True or False? Assume n = 8.
75 > 30 is True, so Python prints "Silver" and skips the elif. The programmer probably wanted "Gold". The fix is to check the bigger value first:
if points > 50:
print("Gold")
elif points > 30:
print("Silver")
B4. Find and fix three errors in this code:
number = int(input("Enter a number: "))
if number = 0
print("Zero")
Solution
The three errors:
= should be == for comparison
The if line is missing a colon :
The print line needs to be indented
number = int(input("Enter a number: "))
if number == 0:
print("Zero")
B5. Write a program that asks the user for a number and tells them if it is positive, negative or zero.
Use if / elif / else.
Solution
number = float(input("Enter a number: "))
if number > 0:
print("Positive")
elif number < 0:
print("Negative")
else:
print("Zero")
(int() instead of float() is fine too.)
B6. Write a program that asks for a whole number and tells the user if it is even or odd. (Hint: use %.)
Solution
number = int(input("Enter a whole number: "))
if number % 2 == 0:
print("Even")
else:
print("Odd")
B7. A cinema charges:
£5 for children under 12
£8 for anyone aged 12 to 64
£6 for anyone aged 65 or over
Write a program that asks for the customer's age and prints the ticket price.
Solution
age = int(input("Enter your age: "))
if age < 12:
print("Ticket price: £5")
elif age < 65:
print("Ticket price: £8")
else:
print("Ticket price: £6")
B8. Write a program that asks the user to enter a username and password. If the username is "admin"and the password is "secret", print "Welcome!", otherwise print "Login failed".
Solution
username = input("Username: ")
password = input("Password: ")
if username == "admin" and password == "secret":
print("Welcome!")
else:
print("Login failed")
Section C: For loops
C1. Write down the numbers produced by each range():
(a) range(4)
(b) range(2, 7)
(c) range(1, 10, 3)
(d) range(10, 4, -2)
Solution
(a) range(4) → 0, 1, 2, 3
(b) range(2, 7) → 2, 3, 4, 5, 6
(c) range(1, 10, 3) → 1, 4, 7
(d) range(10, 4, -2) → 10, 8, 6
C2. How many times will "Hi" be printed?
for i in range(3, 8):
print("Hi")
Solution
5 times (i = 3, 4, 5, 6, 7)
C3. What will this code print?
for i in range(1, 4):
print(i * 10)
Solution
Output:
10
20
30
C4. Complete a trace table for this code and say what is printed at the end.
total = 0
for n in range(2, 9, 2):
total = total + n
print(total)
Solution
Loop
n
total
Start
–
0
1st
2
2
2nd
4
6
3rd
6
12
4th
8
20
It prints 20. (The range stops before 9, so 8 is the last number.)
C5. Write a program that prints the numbers from 1 to 20.
Solution
for number in range(1, 21):
print(number)
C6. Write a program that asks the user which times table they want, then prints it from 1 to 12. For example, if they enter 3:
Output:
1 x 3 = 3
2 x 3 = 6
...
12 x 3 = 36
Solution
table = int(input("Which times table? "))
for i in range(1, 13):
print(i, "x", table, "=", i * table)
C7. Write a program that prints a countdown from 10 to 1 and then prints "Happy New Year!".
Solution
for n in range(10, 0, -1):
print(n)
print("Happy New Year!")
Note that the last print is not indented, so it runs once after the loop finishes.
C8. Write a program that asks the user for a number, then adds up all the whole numbers from 1 up to and including that number. For example, if the user enters 4, the program prints 10 (because 1 + 2 + 3 + 4 = 10).
Solution
limit = int(input("Enter a number: "))
total = 0
for n in range(1, limit + 1):
total = total + n
print(total)
We use limit + 1 because range stops before the end number.
Section D: Challenge questions
D1. FizzBuzz. Print the numbers 1 to 20, except:
If the number is divisible by 3, print "Fizz" instead
If it's divisible by 5, print "Buzz" instead
If it's divisible by both 3 and 5, print "FizzBuzz" instead
Solution
for n in range(1, 21):
if n % 3 == 0 and n % 5 == 0:
print("FizzBuzz")
elif n % 3 == 0:
print("Fizz")
elif n % 5 == 0:
print("Buzz")
else:
print(n)
The "both" check must come first. Otherwise 15 would print "Fizz" and stop there.
D2. Mark collector. Write a program that asks the user for 5 test marks (one at a time, using a loop). At the end, print the total and the average.
Solution
total = 0
for i in range(1, 6):
mark = int(input("Enter mark " + str(i) + ": "))
total = total + mark
average = total / 5
print("Total:", total)
print("Average:", average)
D3. Star triangle. Write a program that asks for a number and prints a triangle of stars. For example, if the user enters 4:
Output:
*
**
***
****
(Hint: remember that "*" * 3 gives "***".)
Solution
rows = int(input("How many rows? "))
for i in range(1, rows + 1):
print("*" * i)
D4. Guessing game. The secret number is 7. Give the user 3 guesses using a for loop. After each guess, tell them "Correct!", "Too high" or "Too low".
Extra challenge: can you stop the game from asking more questions after they guess correctly? Hint: look up the break keyword.
Solution
Basic version:
secret = 7
for attempt in range(3):
guess = int(input("Guess the number: "))
if guess == secret:
print("Correct!")
elif guess > secret:
print("Too high")
else:
print("Too low")
Extra challenge (using break):
secret = 7
for attempt in range(3):
guess = int(input("Guess the number: "))
if guess == secret:
print("Correct!")
break
elif guess > secret:
print("Too high")
else:
print("Too low")
print("Game over")
break stops the loop straight away, so the user won't be asked again after a correct guess.