Simulate Seat Availability Using Loops

Business Scenario

Welcome!

Today is your sixth day as a Junior Data Analyst at the Indian Railways Reservation Department.

The railway reservation system receives continuous booking requests from passengers every day. The operations team wants to simulate the seat booking process, automatically check seat availability, allocate seats one-by-one, and stop accepting reservations once all seats are occupied.

To complete this task successfully, you will need Python

  • Simulate real-time seat booking operations

  • Check seat availability automatically

  • Allocate seats dynamically

  • Stop bookings when seats become full

Pre-Lab Preparation

Topic: Understanding loops in Python

1) Loops in Python (While and For Loop)
2) Range() function
3) Break ,Continue, Pass

Click here to download previous lab file: Python LAB 5

Git Pull

git pull origin branchName

Task 1: Simulate Bookings Using While Loop

In the Railway Reservation System, passengers continuously request ticket bookings until all seats are occupied.

While Loop : A while loop is used to repeatedly execute a block of code as long as a condition is true.

Syntax of While Loop :

while condition:
    # code block

Note

  • The loop executes continuously while the condition remains true.

  • Once the condition becomes false, the loop stops automatically.

The reservation department wants to simulate the booking process where seats are allocated one-by-one automatically.

A while loop is used because the booking process continues until the available seats become zero.

1

Write the Program

2

total_seats = 5

while total_seats > 0:
    print("Seat Booked Successfully")
    print("Remaining Seats:", total_seats - 1)
    total_seats -= 1

print("All Seats Are Filled")

Save the File

seat_booking.ipynb

3

Output

Task 2: Use For Loop and Range() to Check Seat Numbers

For Loop : A for loop is used to execute a block of code repeatedly for a fixed number of times.

Syntax of For Loop :

for variable in sequence:
    # code block

Range() Function :

The range() function is used to generate a sequence of numbers automatically.

Syntax of Range() :

range(start, stop, step)

In the Railway Reservation System, seat numbers are assigned in sequence for every coach.

The railway administrator wants to display all available seat numbers before booking starts. A for loop with the range() function helps generate seat numbers automatically without writing each number manually.

1

Write the Program

print("Available Seat Numbers:")

for seat in range(1, 11):
    print("Seat Number:", seat)

2

Output

Task 3: Stop Booking When Seats Become Full

The break statement is used to immediately stop the loop when a specific condition becomes true.

The continue statement skips the current iteration and moves to the next iteration of the loop.

The pass statement is a placeholder statement that performs no action.

In some situations, the railway reservation system must immediately stop the booking process once all seats are occupied.

The break statement is used to terminate the loop when the seat limit is reached. The continue statement skips a specific iteration, while pass acts as a placeholder for future conditions.

1

Write the Program

total_seats = 5

for booking in range(1, 10):

    if booking > total_seats:
        print("No Seats Available")
        break

    if booking == 3:
        print("Checking Special Reservation")
        continue

    pass

    print("Seat", booking, "Booked Successfully")

2

Output

 

Great job!

You have successfully completed your lab on Simulate Seat Availability Using Loops.In this lab, you used While Loop, For Loop, Range() Function, Break Statement, Continue Statement, Pass Statement

You are now ready to move to the next stage of Junior Data Analyst.

Checkpoint

   Git Push

git push origin branchName

Next-Lab Preparation

Topic: String Handling in Python

1) Introduction to Strings

2) String Manipulation Methods

3) String Formatting Techniques