0% found this document useful (0 votes)
29 views11 pages

Final Project File JJ

Uploaded by

jashraj.s9ngh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views11 pages

Final Project File JJ

Uploaded by

jashraj.s9ngh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Certificate

this is to certify that JASHRAJ SINGH of


CLASS XII A of RAN PUBLIC SCHOOL
has completed project under my
supervision. He has taken interest and has
shown utmost sincerity in completion of
the project work in Computer science as
assigned by CBSE NEW DELHI

---------------
MR. M.C. TIWARI ( HOD CS )
INDEX

1 - Import Modules: The code starts by importing the random and


pickle modules for generating random numbers and working

with binary files.

2 - Function Definitions: It defines three functions:

• generate_ticket_number(): Generates a random ticket


number.
• save_ticket_to_file(ticket_data, filename):
Saves ticket data to a binary file using pickle.
• load_ticket_from_file(filename): Loads ticket
data from a binary file.
3 - Main Function (main()): The core of the program:

• Welcomes the user to the Railway Ticket System.


• Presents available train options in a dictionary.
• Asks the user to select a train and handles input validation.
• Collects user information: name, age, and gender.
• Generates a unique ticket number and creates a ticket data
dictionary.
• Saves the ticket data to a binary file.
• Concludes by informing the user that ticket details are saved.
4 - Conditional Execution : The code checks if it's being run as the
main program and executes the main() function.
PROJECT DETAILS
Aim: The aim of this project is to develop a Railway Ticket System in
Python that allows users to select a train from a list, input their personal
information, and then generates a ticket with details such as the train name,
departure time, ticket number, passenger name, age, gender, and fare. The
system should also save this ticket information to a binary file for future
reference.

Introduction: The Railway Ticket System is designed to simplify the process


of booking and generating railway tickets for passengers. This Python program
provides a user-friendly interface for travelers to choose their preferred train, input
their details, and receive a printed ticket with all the necessary information. The
system uses Python and several modules to accomplish this task efficiently.

About Python: Python is a versatile and high-level programming language


known for its readability and ease of use. It is widely used in various applications,
including web development, data analysis, artificial intelligence, and, as in this case,
developing command-line applications.

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.

Explanation of the Code:


1. generate_ticket_number() : This function generates a random ticket
number between 10 and 9999.
2. save_ticket_to_file(ticket_data, filename) : This function
takes ticket data and a filename as parameters and saves the ticket details to a
binary file using the pickle module.
3. load_ticket_from_file(filename) : This function loads ticket
details from a binary file specified by the filename parameter.
4. main() : The main function of the program, responsible for user interaction
and ticket generation.

• 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.

The code uses a dictionary (train_options) to store information


about available trains, making it easy for users to select a train by number.
It also includes error handling to ensure that users enter valid input. When
executed, this program will generate railway tickets and save them to a
binary file for future reference.
PROJECT CODE

------------------------------------------------------------------------------------------------------------------------

CODE

import random
import pickle

# Function to generate a ticket number

def generate_ticket_number():
return random.randint(10, 9999)

# Function to save ticket details to a binary file

def save_ticket_to_file(ticket_data, filename):


with open(filename, 'wb') as file:
pickle.dump(ticket_data, file)

# Function to load ticket details from a binary file

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']}")

# Ask user to choose a train

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']

name = input("Enter your name: ")


age = input("Enter your age: ")

# Ask user to select gender from available options

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 details as a dictionary

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

You might also like