Skip to content

Instantly share code, notes, and snippets.

@Kahnix
Last active March 27, 2018 11:41
Show Gist options
  • Save Kahnix/163b154d2ae4341855b150e2923427e6 to your computer and use it in GitHub Desktop.
Save Kahnix/163b154d2ae4341855b150e2923427e6 to your computer and use it in GitHub Desktop.
def read_file():
employee_data = {}
try:
with open('salesdata.csv') as csv_file:
csv_reader = csv.reader(csv_file,skipinitialspace=True)
for row in csv_reader:
employee_data[row[0]]=row[1]
return(employee_data)
except FileNotFoundError:
print("Ensure the file 'salesdata' is present and in csv format")
def display_menu():
print("""
==================
Car Sales Terminal
==================
1) Calculate Highest Selling Employee
2) Calculate Total Car Sales
3) Write Option 1 and 2 to a text file
4) Quit""")
handle_menu_choice()
def handle_menu_choice():
user_choice = input("Choose an option (1-4) \n > ")
while not(user_choice.isnumeric()) or int(user_choice) < 1 or int(user_choice) > 4:
print("\n Error: Invalid Input, Please enter numbers between 1 to 4 \n")
user_choice = input("Choose an option (1-4) \n > ")
try:
user_choice = int(user_choice)
user_choice -= 1
except ValueError:
print("Please ensure your input was a number")
menu_options = [calculate_highest_seller, calculate_total_sold,write_to_file,exit]
menu_options[user_choice]()
def calculate_highest_seller():
employee_data = read_file()
highest_sold = 0
name=""
for i in range(len(employee_data.keys())):
for name,key in employee_data.items():
if int(key)>highest_sold:
highest_sold=int(key)
employee_name=name
print("{} Sold the Highest Amount of Cars Totalling {}".format(employee_name, highest_sold))
return[employee_name, highest_sold]
def calculate_total_sold():
total_sales=0
for name, key in employee_data.items():
total_sales+=int(key)
print("The total amount of cars sold were: {} cars".format(total_sales))
return(total_sales)
def write_to_file():
total_sold=calculate_total_sold()
highest_seller=calculate_highest_seller()
f = open('salesdata.txt', 'w')
f.write("{} sold the most cars with {} cars".format(highest_seller[0],highest_seller[1]))
f.write("Total cars sold: {}".format(total_sold))
f.close()
display_menu()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment