



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Topics: Functions, Loops, Selection, Data Structures
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
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
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
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
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