CODE:
import datetime
bus_details = {
"bus_no": "Priya Travels",
"source": None,
"destination": None,
"total_seats": 60,
"booked_seats": [],
"ticket_price": 500
}
def set_bus_route():
print("\n** Set Bus Route **")
bus_details['source'] = input("Enter Source: ")
bus_details['destination'] = input("Enter Destination: ")
print(f"\nRoute Set: {bus_details['source']} -> {bus_details['destination']}")
def display_bus_details():
print("\n*** Bus Details ***")
print(f"Bus Number: {bus_details['bus_no']}")
print(f"Route: {bus_details['source']} -> {bus_details['destination']}")
print(f"Total Seats: {bus_details['total_seats']}")
print(f"Available Seats: {bus_details['total_seats'] - len(bus_details['booked_seats'])}")
def generate_bill_receipt(name, gender, age, num_seats, seat_numbers, total_amount):
print("\n--- Payment Receipt ---")
print(f"Passenger Name: {name}")
print(f"Gender: {gender}")
print(f"Age: {age}")
print(f"Bus Number: {bus_details['bus_no']}")
print(f"Route: {bus_details['source']} -> {bus_details['destination']}")
print(f"Number of Seats Booked: {num_seats}")
print(f"Seat Numbers: {', '.join(map(str, seat_numbers))}")
print(f"Total Amount Paid: ₹{total_amount}")
print(f"Booking Date and Time: {datetime.datetime.now().strftime('%d-%m-%y %H:%M:%S')}")
print("\nBooking Successful!")
print("-----------------------")
def book_ticket():
if not bus_details['source'] or not bus_details['destination']:
print("\nRoute not set! Please set the source and destination first.")
return
display_bus_details()
if len(bus_details['booked_seats']) >= bus_details['total_seats']:
print("\nSorry, no seats are available!")
return
name = input("\nEnter Passenger Name: ")
gender = input("Enter Gender (Male/Female/Other): ")
age = input("Enter Age: ")
try:
age = int(age)
if age <= 0:
print("Invalid age. Please enter a valid positive number.")
return
except ValueError:
print("Invalid input for age. Please enter a number.")
return
num_seats = int(input("Enter Number of Seats to Book: "))
if num_seats > (bus_details['total_seats'] - len(bus_details['booked_seats'])):
print("\nNot enough seats available!")
return
seat_numbers = []
for _ in range(num_seats):
for seat in range(1, bus_details['total_seats'] + 1):
if seat not in bus_details['booked_seats']:
bus_details['booked_seats'].append(seat)
seat_numbers.append(seat)
break
total_amount = num_seats * bus_details['ticket_price']
generate_bill_receipt(name, gender, age, num_seats, seat_numbers, total_amount)
def main_menu():
while True:
print("\n**** Bus Ticket Booking System ****")
print("1. Set Bus Route")
print("2. View Bus Details")
print("3. Book a Ticket")
print("4. Exit")
choice = input("\nEnter your choice: ")
if choice == "1":
set_bus_route()
elif choice == "2":
display_bus_details()
elif choice == "3":
book_ticket()
elif choice == "4":
print("\nThank you for using the Bus Ticket Booking System!")
break
else:
print("\nInvalid choice! Please try again.")
if __name__ == "__main__":
main_menu()