Skip to content

Instantly share code, notes, and snippets.

@rawnly
Created April 30, 2020 13:15
Show Gist options
  • Save rawnly/3711b6c4dd986661f36559fe88833cd7 to your computer and use it in GitHub Desktop.
Save rawnly/3711b6c4dd986661f36559fe88833cd7 to your computer and use it in GitHub Desktop.
from statistics import median
d={}
flag=True
def load_from_file(filename):
try:
# Load data from file
f=open(filename, "r")
data = {}
rows = f.readlines()
for row in rows:
row = row.strip().split("=")
data[row[0]] = ""
return True
except:
print("File '" + filename + "' not found.")
return False
while flag:
command=input("Please enter your command: ").split(" ")
if command[0].lower()=="exit":
flag=False
print("Console is exiting, bye bye!")
elif command[0].lower()=="add":
if d.get(command[1].lower())!=None:
print("Sorry, this user already exists!")
else:
d[command[1]] = []
print("User created correctly!")
elif command[0].lower()=="sales":
if d.get(command[1].lower())!=None:
d[command[1]].append(int(command[2]))
print("Sale saved correctly!")
else:
print("Sorry, user does not exist!")
elif command[0].lower()=="save":
f = open("SalesBBA.txt","a")
for i in d.keys():
f.write(i+"="+str(d[i])+"\n")
f.close()
print("Sales saved correctly!")
elif command[0].lower()=="clear":
d = {}
try:
f = open("SalesBBA.txt","w")
f.close()
print("File and Console memory correctly cleared!")
except:
f = open("SalesBBA.txt", "a")
f.close()
print("File not found. Console memory cleared.")
elif command[0].lower()=="load":
if load_from_file("SalesBBA.txt"):
print("Sales correctly loaded!")
elif command[0].lower()=="report":
print("Customer; Average Sales")
for customer in d.keys():
if len(d[customer]) == 0:
print(customer, "=", 0)
else:
print(customer, "=", median(d[customer]))
else:
print("Command not found")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment