苏北人 2021-07-01 12:47 采纳率: 100%
浏览 42
已采纳

我怎么样写这个程序,我不是很明白怎么写

我刚学python没多久,我们老师基本上什么也不教,就让我们自学,这是我们其中一次作业,我不知道怎么写 这个是csv文件数据 img

    # ------------------------------- Imported Packages -------------------------------
    import csv
    import matplotlib.pyplot as plt

    # ----------------------------- Function Definitions ------------------------------

    def menu():
# Display menu
print("Used Car Sales")
print("\n1. Import sales data from csv") # import sales data
print("2. Calculate total sales for each employee") # calculate total sales of employees
print("3. Calculate mean sales for each employee") # calculate the monthly mean sales for each employee
print("4. Graph of monthly sales data") # Create graph of monthly sales data
print("5. Exit") # Exit program

option = -1 # used to store the user's choice

# while x is less than 1 or greater than 5, loop
# used to validate user input
while option < 1 or option > 5:    
    # get user's choice
    option = int(input("\nwhich operation do you want to perform?\n"))

return option

    # ---------------------------------------------------------------------------------

    # function which will open the csv file and read in its values and return a list containing each record as a dictionary
    def open_csv_file(file_path):

database = [] # create empty list 

with open(file_path,"r") as csv_file: # open file in read mode
    # use DictReader to read in the data
    csv_reader = csv.DictReader(csv_file, delimiter=",")
    
    # add each line, as a dictionary, to the list 'database'
    for line in csv_reader:
        database.append(dict(line))
    print(database)
    # return the database
    return database

    # ---------------------------------------------------------------------------------

    # finds and returns a list of the total sales for each employee
    def calc_total_sales(database):
# create empty list
total_sales = []
# iterate through each record
for num in total_sales:
    num = 0
    print(num)
    # initialise a variable to track the total

    # loop through each key (need to ignore name key) and add to total
    # append total value to total_sales list
    
    
# return list of total sales for each employee
return total_sales

    # ---------------------------------------------------------------------------------

    def calc_mean_sales(sales_list):
pass # DELETE THIS AND ADD YOUR CODE HERE

    # ---------------------------------------------------------------------------------

    def show_graph(database):

x_values = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"] # COMPLETE THIS LIST
y_values = [] # used to store y values

# ADD SOME CODE TO LOOP THROUGH ALL EMPLOYEES - CURRENTLY THIS ONLY WORKS FOR FIRST EMPLOYEE
    
# get sales data and store in y_values list
for key in x_values:
     y_values.append(float(database[0][key])) # will need to modify this for all employees

# add data line to the graph
plt.plot(x_values,y_values,'b',label=database[0]["Name"]) # will need to modify this for all employees

# set graph attributes
plt.xlabel("X AXIS")
plt.ylabel("Y AXIS")
plt.title('SUITABLE TITLE')
plt.legend()
plt.show()
plt.ion()

# pause the program to show graph
input("enter to return to menu...")
plt.close('all')

    # -------------------------- Initialise Global Variables --------------------------

    sales_data = [] # list to store the sales data as a dictionary
    total_sales = [] # list to store the total sales for employees
    mean_sales = [] # list to store the mean sales for employees

    read_file_path = "sales_data.csv" # file path for file to be read

    # ---------------------------------- Main Program ---------------------------------

    end_program = False # variable used to check if user wants to exit program

    # loop until end_program is True
    while not end_program:

# call menu function and get user's choice
option = menu()

if option == 1: # import data from csv
    sales_data = open_csv_file(read_file_path)
    
    # used for debug only, DELETE once program complete
    for record in sales_data:
        print(record)
    
elif option == 2: # Calculate total sales for each employee
    total_sales = calc_total_sales(sales_data)
    
    # Show total sales
    print(total_sales)
    
elif option == 3: # Calculate mean sales for each employee
    mean_sales = calc_mean_sales(total_sales)
    
elif option == 4: # Create graph of monthly sales
    show_graph(sales_data)
    
else: # option 5 - exit in controlled manner
    
    # set end_program Boolean to True
    end_program = True
    
    # output messages to user
    print("Thank you for using this program")
    print("Quitting Program...")
  • 写回答

2条回答 默认 最新

  • CSDN专家-HGJ 2021-07-01 14:21
    关注

    计算每人销售总额和每人平均销售额两个函数可以这样写:

    def calc_total_sales(database):
        # create empty list
        total_sales = []
        # iterate through each record
        for num in datebase:
            s=0
            for k,v in num.items():
                if k=='Name':
                    continue
                else:
                    s += float(v)
            total_sales.append(dict({num['Name']: s}))
        return total_sales
    def calc_mean_sales(sales_list):
        mean_sales = []
        for x in sales_list:
            for k,v in x.items():
                mean_sales.append(dict({k: round(v/12, 2)}))
        return mean_sales
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败