Skip to content

Instantly share code, notes, and snippets.

@peadar
Created September 19, 2024 15:32
Show Gist options
  • Save peadar/f9cc94559b4585cebf85915e5c1c101a to your computer and use it in GitHub Desktop.
Save peadar/f9cc94559b4585cebf85915e5c1c101a to your computer and use it in GitHub Desktop.
from json import dump, load
import os
price = 10 #ticket price
PATH = "C:/Users/anaki/OneDrive/TY musical/ticketSales.json"
PATH = "./data.json"
#load
try:
with open( PATH, "r") as file:
names = load(file)
except:
names = [
[ { "name": "Sample Name 1", "email": "name@sample1" } ],
[ { "name": "Sample Name 2", "email": "name@sample1" } ],
[ { "name": "Sample Name 3", "email": "name@sample1" } ],
]
while True:
run = input("what would you like to do? ") #user chooses action
#sell tickets
if run == "sell":
name = input("name? ") #name of buyer
email = input("email? ") #name of buyer
night = int(input("night? "))-1 #night of the musical, 3rd dec = night1 ...etc
entry = { "name": name, "email": email }
names[night].append( entry ) #adds name to list
print("ticket number:",len(names[night])) #ticket number is just where you are on the list
try:
os.rename(PATH, PATH + ".backup" )
except:
pass
with open(PATH, "w", encoding="utf-8") as file:
dump(names, file, indent=1) #saves to json file
#shows list of who has what ticket and how much money there should be
if run == "check":
for j in range (3):
print("night:",j+1)
print(f"{'name':25}{'email':25}{'ticket num':<25}")
print(f"{'====':25}{'=====':25}{'==========':<25}")
for i in range (len(names[j])):
#print(names[j][i]["name"],"\t",i)
print( f'{names[j][i]["name"]:25}{names[j][i]["email"]:25}{i:<25}')
# names[j][i]["name"],"\t",i)
print("€",(len(names[j])-1)*price) #money from one night
print()
print("€",(len(names[0]+names[1]+names[2])-3)*price," total") #money from all nights
print() #blank line for neater output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment