Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Train Ticket Booking System

Source code:

def display_seats(seat_matrix):

for row in seat_matrix:

print(" ".join(row))

def book_seats(seat_matrix, num_seats):

for i in range(len(seat_matrix)):

for j in range(len(seat_matrix[0])):

if seat_matrix[i][j] == 'O':

seat_matrix[i][j] = 'X'

num_seats -= 1

if num_seats == 0:

return True

return False

def train_reservation():

rows = 5

cols = 10

seat_matrix = [['O' for _ in range(cols)] for _ in range(rows)]


while True:

print("\nCurrent Seat Matrix:")

display_seats(seat_matrix)

num_seats = int(input("\nEnter the number of seats to book (0 to exit): "))

if num_seats == 0:

print("Exiting program.")

break

if num_seats > rows * cols:

print("Not enough seats available.")

continue

if book_seats(seat_matrix, num_seats):

print(f"Successfully booked {num_seats} seat(s).")

else:

print("Not enough consecutive seats available.")

train_reservation()
Output:

Welcome to Train ticket booking system

Current Seat Matrix:

OOOOOOOOOO

OOOOOOOOOO

OOOOOOOOOO

OOOOOOOOOO

OOOOOOOOOO

Enter the number of seats to book (0 to exit): 5

Successfully booked 5 seat(s).

Welcome to Train ticket booking system

Current Seat Matrix:

XXXXXOOOOO

OOOOOOOOOO

OOOOOOOOOO

OOOOOOOOOO

OOOOOOOOOO

Enter the number of seats to book (0 to exit): 44

Successfully booked 44 seat(s).


Welcome to Train ticket booking system

Current Seat Matrix:

XXXXXXXXXX

XXXXXXXXXX

XXXXXXXXXX

XXXXXXXXXX

XXXXXXXXXO

Enter the number of seats to book (0 to exit): 2

Not enough consecutive seats available.

Welcome to Train ticket booking system

Current Seat Matrix:

XXXXXXXXXX

XXXXXXXXXX

XXXXXXXXXX

XXXXXXXXXX

XXXXXXXXXX

Enter the number of seats to book (0 to exit): 0

Exiting program.

You might also like