Challenge 1: Grocery Shopping - Calculating Total Cost
Objective: Write a Python program that calculates the total cost of grocery items.
Problem Description:
You have a list of grocery item prices, but they are stored as strings. You need to calculate the total cost of all the items by converting these string values into integers and summing them up.
Requirements:
- Create a list called
grocery_prices
containing the following prices (in string format):["3", "4", "5", "2", "10"]
. - Use a
for
loop to iterate through each price, cast each price to an integer, and then calculate the total cost. - At the end of the loop, display the total cost by concatenating the result with a string message (e.g., "Total cost of groceries: £24").
Hints:
- Use
int()
to cast strings to integers. - You can concatenate strings using the
+
operator. - You will need a variable to store the total cost, initialised to 0.
Challenge 2: Student Grades - Counting Passes
Objective: Write a Python program that counts how many students passed a test.
Problem Description:
You are given a list of student grades. A student is considered to have passed if their grade is 50 or more. Your task is to count how many students passed.
Requirements:
- Create a list called
grades
containing the following values:[45, 67, 89, 38, 50, 92, 76]
. - Use a
for
loop to iterate through the list and use anif
statement to check if the grade is greater than or equal to 50 (this means the student passed). - Keep a counter to track how many students passed.
- At the end of the loop, print a message that shows the number of students who passed, using concatenation to combine the result with the message.
Hints:
- You can use the
>=
operator to check if a grade is 50 or more. - Initialise a variable (e.g.,
pass_count
) to zero before the loop to count the passing students. - Use
str()
to cast numbers to strings when concatenating.
Challenge 3: Car Rental Cost Calculation
Objective: Write a Python program to calculate the total rental cost for customers based on the number of days they rented a car.
Problem Description:
A car rental service charges £40 per day for each car rented. You are provided with a list of customers, each of whom rented a car for a certain number of days. Your task is to calculate and display the total rental cost for each customer.
Requirements:
- Create a list called
rental_days
with the following values:[2, 5, 1, 3, 7]
. Each value represents the number of days a customer rented the car. - Use a
for
loop to iterate through the list of rental days. - For each number of days, calculate the total cost by multiplying the number of days by the daily rate (which is 40).
- Display a message showing the number of rental days and the total cost, using string concatenation.
Hints:
- Use
*
to multiply the number of rental days by the daily rate (40). - You can convert numbers to strings using
str()
for concatenation.