Scenario: A small cinema has 4 rows of 5 seats. In seating_chart, 0 means empty and a positive number is the price paid (in dollars) for a booked seat.
Data
seating_chart = [
# Seat 1, Seat 2, Seat 3, Seat 4, Seat 5
[12, 0, 12, 15, 0], # Row 1 (Front)
[15, 15, 0, 15, 15], # Row 2
[18, 18, 18, 18, 18], # Row 3
[0, 20, 20, 0, 20] # Row 4 (Back)
]
Your tasks
- Counting — count the total number of booked seats (value greater than 0) in the whole cinema.
- Totalling by row — for each row, print that row’s sales revenue.
- Averaging a column — average price for Seat 3 (column index 2), only counting booked seats (price > 0).
- Maximum and minimum — find the most expensive ticket and the cheapest booked ticket (ignore empty seats valued 0).
💡 Try every task in your editor first. Only reveal the solution when you have had a proper go.
Solution
seating_chart = [
[12, 0, 12, 15, 0],
[15, 15, 0, 15, 15],
[18, 18, 18, 18, 18],
[0, 20, 20, 0, 20]
]
# Challenge 1: Count booked seats
booked = 0
for row in seating_chart:
for price in row:
if price > 0:
booked = booked + 1
print("Number of booked seats:", booked)
# Challenge 2: Revenue per row
for r in range(len(seating_chart)):
row_total = 0
for price in seating_chart[r]:
row_total = row_total + price
print("Row", r + 1, "Revenue: $" + str(row_total))
# Challenge 3: Average booked middle seats (column 2)
middle_total = 0
middle_count = 0
for r in range(len(seating_chart)):
price = seating_chart[r][2]
if price > 0:
middle_total = middle_total + price
middle_count = middle_count + 1
print("Average price for booked middle seats:", middle_total / middle_count)
# Challenge 4: Max and min booked ticket (ignore empty seats)
max_price = seating_chart[0][0]
min_price = seating_chart[0][0]
for row in seating_chart:
for price in row:
if price > 0:
if price > max_price:
max_price = price
if min_price == 0 or price < min_price:
min_price = price
print("Most expensive ticket: $" + str(max_price))
print("Cheapest booked ticket: $" + str(min_price))
Example output:
Number of booked seats: 15
Row 1 Revenue: $39
Row 2 Revenue: $60
Row 3 Revenue: $90
Row 4 Revenue: $60
Average price for booked middle seats: 16.666666666666668
Most expensive ticket: $20
Cheapest booked ticket: $12
💡 Seat 3 is column index
2. Rows 1, 3 and 4 are booked there ($12, $18 and $20), so the average is 50 / 3.