Final Project File JJ
Final Project File JJ
---------------
MR. M.C. TIWARI ( HOD CS )
INDEX
Modules Used:
1. random : This module is used to generate random ticket numbers for each
booking. It ensures that ticket numbers are unique and unpredictable.
2. pickle : The pickle module is employed for serializing and deserializing
Python objects. In this program, it's used to save and load ticket data in a
binary file.
• It displays a list of available trains with their names, fares, and departure
times.
• It asks the user to select a train by entering the corresponding number.
• It collects user information such as name, age, and gender.
• It generates a unique ticket number and creates a dictionary
containing all ticket information.
• Finally, it saves the ticket information to a binary file using the
save_ticket_to_file() function.
------------------------------------------------------------------------------------------------------------------------
CODE
import random
import pickle
def generate_ticket_number():
return random.randint(10, 9999)
def load_ticket_from_file(filename):
with open(filename, 'rb') as file:
loaded_ticket_data = pickle.load(file)
return loaded_ticket_data
def main():
print("Welcome to the Railway Ticket System!")
train_options = {
1: {"name": "rajdhani Train", "fare": 500, "time": "09:00 AM"},
2: {"name": "shatabdi Train", "fare": 700, "time": "12:30 PM"},
3: {"name": "Local Train", "fare": 200, "time": "03:15 PM"},
4: {"name": "vandabharat Train", "fare": 1000, "time": "06:45 PM"},
}
print("Available Trains:")
for key, value in train_options.items():
print(f"{key}. {value['name']} - Fare: Rs {value['fare']} - Departure Time:
{value['time']}")
while True:
try:
choice = int(input("Enter the number corresponding to your desired train: "))
if choice in train_options:
break
else:
print("Invalid choice. Please choose a valid train number.")
except ValueError:
print("Invalid input. Please enter a number.")
train_info = train_options[choice]
train_name = train_info['name']
fare = train_info['fare']
departure_time = train_info['time']
while True:
gender = input("Enter your gender (male/female/others): ")
if gender.lower() in ['male', 'female', 'others']:
break
else:
print("Invalid input. Please enter 'male' or 'female'.")
ticket_number = generate_ticket_number()
ticket_data = {
"Train Name": train_name,
"Departure Time": departure_time,
"Ticket Number": ticket_number,
"Name": name,
"Age": age,
"Gender": gender.capitalize(),
"Fare": fare
}
print(ticket_data)
# Save ticket details to a binary file
save_ticket_to_file(ticket_data, 'ticket_data.bin')
print("Ticket details saved to ticket_data.bin")
if __name__ == "__main__":
OUTPUT