Form 4 - Computer

2024-2025

Floor 4 - Computer Room

Mr. Peter

Nested While Loops

Santa Rosa de Lima English Secondary School

聖羅撒英文中學

Colégio de Santa Rosa de Lima - Secção Inglesa

Python Exercises

Python Nested Loops - Ex01

Cinema Seating Optimization

1.

Save your file as "ClassNumber_YourName_Ex01.py"

A cinema hall has 10 rows, each with 15 seats. For each show, fill each seat with alternating people and leave a gap every 3rd seat for spacing. (P for person, _ for empty)

You are given three variables: rows, seats_per_row.

1

6

Output Example

P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
rows = 10
seats_per_row = 15

Use a while loop to iterate over all the rows from 0 to rows - 1.

2

Use a nested while loop to iterate over seat numbers from 0 to seats_per_row - 1.

3

For each seat, print 'P' to row unless the seat index is a multiple of 3 (starting from 0), in which case print '_'. After filling a row.

4

Within each printed row, print the seat characters with a space in between.

5

print( "P ", end="" )

Python Nested Loops - Ex01

Cinema Seating Optimization

1.

Save your file as "ClassNumber_YourName_Ex01.py"

A cinema hall has 10 rows, each with 15 seats. For each show, fill each seat with alternating people and leave a gap every 3rd seat for spacing. (P for person, _ for empty)

You are given three variables: rows, seats_per_row.

1

6

Output Example

P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
P P _ P P _ P P _ P P _ P P _
rows = 10
seats_per_row = 15

Use a while loop to iterate over all the rows from 0 to rows - 1.

2

Use a nested while loop to iterate over seat numbers from 0 to seats_per_row - 1.

3

For each seat, print 'P' to row unless the seat index is a multiple of 3 (starting from 0), in which case print '_'. After filling a row.

4

Within each printed row, print the seat characters with a space in between.

5

print( "P ", end="" )

Python Nested Loops - Ex02

Catching a Hit-and-Run Driver

1.

Save your file as "ClassNumber_YourName_Ex02.py"

A truck violated traffic rules and fled the scene after hitting a pedestrian. There were three eyewitnesses at the scene. Although none of them could remember the full license plate number, they each provided some useful clues: 

Witness A said: “The first two digits of the license plate number are the same.”

1

Witness B said: “The last two digits of the license plate number are the same, but different from the first two digits.”

2

Witness C, a mathematician, said: “The 4-digit license plate number is a perfect square.”

3

Based on these clues, write the python to get the license plate number.