Docsity

Thinking Like a Programmer Python Exam Practice Functions, Loops, Selection & Data Str, Exams of Computer Science

Toggle sidebar
bg1
Thinking Like a Programmer — Python
Original Exam-Style Practice Questions
Topics: Functions, Loops, Selection, Data Structures
Question 1 — Functions
Problem 1:
A shop gives customers a discount depending on the amount they spend:
£100 or more → 20% discount
£50–£99 → 10% discount
Below £50 → no discount
Write a function that calculates and displays the final price.
Python Code:
def calculate_price(price):
if price >= 100:
discount = price * 0.20
elif price >= 50:
discount = price * 0.10
else:
discount = 0
final_price = price - discount
return final_price
price = float(input("Enter the price: "))
print("Final price:", calculate_price(price))
pf3
pf4
pf5

Partial preview of the text

Download Thinking Like a Programmer Python Exam Practice Functions, Loops, Selection & Data Str and more Exams Computer Science in PDF only on Docsity!

Thinking Like a Programmer — Python

Original Exam-Style Practice Questions

Topics: Functions, Loops, Selection, Data Structures

Question 1 — Functions

Problem 1:

A shop gives customers a discount depending on the amount they spend:  £100 or more → 20% discount  £50–£99 → 10% discount  Below £50 → no discount Write a function that calculates and displays the final price. Python Code: def calculate_price(price): if price >= 100: discount = price * 0. elif price >= 50: discount = price * 0. else: discount = 0 final_price = price - discount return final_price price = float(input("Enter the price: ")) print("Final price:", calculate_price(price))

Output: Enter the price: 120 Final price: 96. Question 2 — Loops

Problem 2:

A lecturer has recorded the following student marks. The program should count how many students passed. A pass is a mark of 40 or above. Python Code: marks = [45, 67, 32, 81, 56, 29, 74] passes = 0 for mark in marks: if mark >= 40: passes += 1 print("Number of passes:", passes) Output: Number of passes: 5 Question 3 — Selection

Problem 3:

A cinema classifies customers based on their age:  Under 13 → Child  13–17 → Teenager

print("Sara's mark:", students["Sara"]) students["Lina"] = 67 print("Students who achieved 60 or above:") for name, mark in students.items(): if mark >= 60: print(name) Output: Sara's mark: 84 Students who achieved 60 or above: Amira Sara Lina Question 5 — Functions and Loops

Problem 5:

A program receives a list of numbers. Create a function that calculates the total of all the numbers in the list. Python Code: def calculate_total(numbers): total = 0 for number in numbers: total += number return total

numbers = [5, 10, 15, 20] result = calculate_total(numbers) print("Total:", result) Output: Total: 50 Question 6 — Selection and Loops

Problem 6:

A program asks the user to enter five numbers. It should count how many of the numbers are positive. Python Code: positive = 0 for i in range(5): number = int(input("Enter a number: ")) if number > 0: positive += 1 print("Positive numbers:", positive) Output: Enter a number: 4 Enter a number: - Enter a number: 8 Enter a number: 0 Enter a number: 3