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

## Arrays set at the start of the program

## Up train arrays
UpTrain = ["09:00","11:00","13:00","15:00"] ## train times for up train
UpMoney = [0,0,0,0] ## Total money for each up journey
UpPassengers = [0,0,0,0] ## Total passengers for each up journey
UpSeats = [480,480,480,480] ## Seats available for each up journey

## Same as above but for down journeys


DownTrain = ["10:00","12:00","14:00","16:00"]
DownMoney = [0,0,0,0]
DownPassengers = [0,0,0,0]
DownSeats = [480,480,480,640]

cost = (25) ## Constant variable: cost of 1 ticket


## Below are variables I used for my own convenience
invalid = ("Invalid input.")
visual_num = [1,2,3,4]
closed = ("CLOSED")

import os ## to clear the screen at the start of the program


os.system('clear')

def display(): ## function to show the screen display


for i in range(len(UpSeats)): ## for loop for the number of values in
UpSeats
if UpSeats[i] == 0:
UpSeats[i] = ("CLOSED") ## When there are no seats available (0),
the value '0' is changed to 'CLOSED'
for i in range(len(DownTrain)):
if DownSeats[i] == 0:
DownSeats[i] = ("CLOSED") ## same but for down seats
print("-----TRAIN JOURNEY DISPLAY----- \nLEAVING TRAINS:")
for i in range(len(UpTrain)): ## i starts at 0, and for every loop, i
increases by 1 until 3
print(str("Journey No: "+str(visual_num[i])+" | Time: "+UpTrain[i]+
" | Tickets available: "+str(UpSeats[i]))) ## prints the first item of
array, and then i increases by 1, which means the program prints the
second item of the array afterwards and so on, until it goes through
the enter array
print("\nRETURNING TRAINS:")
for i in range(len(DownTrain)): ## same code as above but for down
trains
print(str("Journey No: "+str(visual_num[i])+" | Time:
"+DownTrain[i]+ " | Tickets available: "+str(DownSeats[i])))

display() ## runs the display() function at the start of the program

def statistics():
global MaxJourney
global DirectionTravel

print("\n----\nEND OF DAY\n\n-----TRAIN JOURNEY


DISPLAY----\n\nLEAVING TRAINS:")
for i in range(len(UpTrain)): ## same as original train display, but
it prints Total money and total passengers for each train journey
instead
print(str("Journey No: "+str(visual_num[i])+" | Time: "+UpTrain[i]+
" | Total money: "+str(UpMoney[i])+ " | Passengers:
"+str(UpPassengers[i])))
print("\nRETURNING TRAINS:")
for i in range(len(DownTrain)):
print(str("Journey No: "+str(visual_num[i])+" | Time:
"+DownTrain[i]+ " | Total money: "+str(DownMoney[i])+ " | Passengers:
"+str(DownPassengers[i])))

print("\nTotal number of passengers: "+str(TotalPassengers)+ "\nTotal


money: $"+str(TotalMoney)) ## prints total passengers and total money
for all journeys combined

MaxPassengers = 0 ## creates MaxPassengers variable for below, which


is first set at 0
MaxJourney = ("N/A") ## if the user does not purchase any tickets, it
inputs this
DirectionTravel = ("N/A") ## same as above
for i in range(len(UpPassengers)):
if UpPassengers[i] > MaxPassengers: ## starts at the first item of
UpPassengers array, and if that item value is greater than
MaxPassengers (starts at 0), it replaces it. Loops through the whole
array to find the max.
MaxPassengers = UpPassengers[i]
MaxJourney = UpTrain[i] ## sets the specific train journey when a
replacement of 'MaxPassengers' occurs
DirectionTravel = ("UP") ## if any replacement occurs here if
'MaxPassengers', it is 'UP'as the program has not checked
'DownPassengers' yet.
for i in range(len(DownPassengers)): ## same as above, but for
'DownPassengers' array
if DownPassengers[i] > MaxPassengers:
MaxPassengers = DownPassengers[i]
DirectionTravel = ("DOWN")
MaxJourney = DownTrain[i]
print("\nThe journey with the most passengers today:\n-> Time:
"+str(MaxJourney)+"\n-> Direction of travel: "+str(DirectionTravel)+
"\n-> Number of passengers: "+str(MaxPassengers)) ## prints the results
from above loops

TotalPassengers = TotalMoney = FreeTickets = 0 ## these variables are


set at 0
def calculations():
global NoPassengers ## 'global' ensures the variables used in the
function is allowed to be used outside of the function
global TotalPassengers
global TotalMoney
global FreeTickets
if NoPassengers >= 10 and NoPassengers <= 80: ## 10 and 80 passengers
inclusive get a free ticket for every 10th passenger - checks to see if
the tickets purchased fits the requirement
FreeTickets = NoPassengers // 10 ## used to calculate the number of
free tickets. Floor division is used - no remainder or decimals
OneWayCost = (NoPassengers - FreeTickets) * cost ## one way cost
TotalCost = (OneWayCost*2) ## two way cost - this is necessary as one
passengers must purchase their departure and return ticket together
print("\n----\nTotal price: $"+str(TotalCost)+"\nEnjoy your
trip!\n\n----") ## outputs cost
TotalPassengers = TotalPassengers + NoPassengers ## A totalling
variable for TotalPassengers
TotalMoney = TotalMoney + TotalCost ## A totalling variable for
TotalMoney
## Below are used to update the arrays
UpMoney[UpJourney] = UpMoney[UpJourney] + OneWayCost
UpPassengers[UpJourney] = UpPassengers[UpJourney] + NoPassengers
UpSeats[UpJourney] = UpSeats[UpJourney] - NoPassengers
DownMoney[DownJourney] = DownMoney[DownJourney] + OneWayCost
DownPassengers[DownJourney] = DownPassengers[DownJourney] +
NoPassengers
DownSeats[DownJourney] = DownSeats[DownJourney] - NoPassengers
FreeTickets = 0 ## resets the free tickets to 0 so the next purchase
will not have free tickets based on the previous user
display() ## calls the train display function and then the choice
function - reset
choice()

def TicketPurchase():
global UpJourney
global DownJourney
global NoPassengers
UpJourney = UpJourney - 1 ## Allows UpJourney to align with the
arrays, as the first item of arrays has a position of 0 and not 1
DownJourney = DownJourney - 1 ## same as above
while True:
try:
NoPassengers = int(input("\n> Groups of between 10 and 80
inclusive get a free ticket for every tenth passenger < \nInput number
of passengers: "))
if UpSeats[UpJourney] == (closed) or DownSeats[DownJourney] ==
(closed): ## train journeys with 0 seats available are changed to
CLOSED
print("Sorry, seats not available for chosen time(s). Refer to
train display.\n----")
choice()
break
if NoPassengers <= 0: ## user cannot purchase 0 tickets
print("Input value greater than 0.")
continue
if NoPassengers > UpSeats[UpJourney] or NoPassengers >
DownSeats[DownJourney]: ## checks if seats are available by comparing
input value with UpSeats/DownSeats array
print("Sorry, seats not available for chosen time(s). Refer to
train display.\n----")
choice()
break
verification()
break
except ValueError:
print(invalid)

def verification(): ## allows user to verify/confirm their purchase -


to make sure user does not input anything incorrect
while True:
PurchaseVerification = input("\n----\nPlease verify your purchase:
\n-> Leaving train time: "+str(UpTrain[UpJourney])+"\n-> Return train
time: "+str(DownTrain[DownJourney])+"\n-> No. passengers:
"+str(NoPassengers)+"\n-> No. tickets purchased:
"+str((NoPassengers)*2)+"\n\n[ 1 ] to confirm\n[ 2 ] to cancel\nInput
number based on your choice: ")
if PurchaseVerification == ("1"):
calculations()
break
elif PurchaseVerification == ("2"):
print("\n")
display()
choice()
break
else:
print("\nInput according to prompts above.")
continue

def UpInput():
global UpJourney ## allows UpJourney variable to be used outside of
the function
while True:
try: ## try and except cancels input of the wrong data type,
allowing the program to not stop completely (ValueError)
UpJourney = int(input("\nWhich train journey UP the mountain - [
1, 2, 3, or 4 ] : "))
if UpJourney > 4 or UpJourney < 1: ## there are only train
journeys 1, 2, 3, and 4
print("Input train journey 1, 2, 3, or 4.")
continue
DownInput() ## calls DownInput function
break ## stops/breaks the while loop
except ValueError:
print(invalid)

def DownInput():
global DownJourney ## allows DownJourney variable to be used outside
of the function
while True:
try:
DownJourney = int(input("\nWhich train journey DOWN the mountain
- [ 1, 2, 3, or 4 ] : "))
if DownJourney > 4 or DownJourney < 1:
print("That is not a existing journey.")
continue
elif DownJourney < UpJourney:
print("This journey is not available as it is before your
journey up the mountain.") ## user cannot purchase a down time that is
before the up time.
continue ## goes back to the start of the While loop
TicketPurchase() ## calls TicketPurchase function
break
except ValueError:
print(invalid)

def choice():
while True:
Choice = input("\n----\n[ 1 ] to purchase tickets\n[ 2 ] to proceed
to end of day \nInput number based on your choice: ") ## gives user
choice of whether they want to purchase tickets or not
if Choice == ("1"):
UpInput() ## starts purchasing process by calling UpInput
function
break
elif Choice == ("2"):
statistics() ## Ends purchasing opportunity and displays the
statistics for the day by calling statistics function
break
else:
print("Input according to prompts above.")
continue

choice()

You might also like