使用循环,元组,列表,字典等的销售计算器

我正在尝试制作一个销售计算器,以显示不同的内容,例如一周的最大销售额是多少,哪一天是多少,一周的最小销售额是多少,哪一天是什么,总金额是多少所有天的销售额的总和,一周的平均销售额以及不到100美元的销售佣金为0美元,100美元至250美元之间的销售额为25美元,250美元至500美元之间的销售额为30美元以及40美元销售额超过500美元。

我尝试了不同的方法来计算平均值,但无法使平均值起作用,我不确定如何在销售佣金中工作以及将最小值和最大值与它们发生的星期几相关联上。

这是我目前的状态:

print ("Sales Calculator Program")

print ('\n')


expenses = []
for day_number in range (1,5 + 1):
    while True:
        user_input = float(input(f"Enter sales for day {day_number}\n> "))
        if user_input >= 0:
            expenses.append(user_input)
            break
        else:
            print(f"Amount may not be negative. Try again:")


print ('\n')       

average = average(expenses)
finalExpenses = sum(expenses)


print ("Total weekly sales were $" +str(finalExpenses))
print ("Average of the sales is $" +str(average))

这就是我要使其看起来像的东西

Enter sales for day 1: 10.22 (User input)
Enter sales for day 2: 4.12 (User input)
Enter sales for day 3: 3.78 (User input)
Enter sales for day 4: 6.82 (User input)
Enter sales for day 5: 22.45 (User input)

Maximum sales was on Friday which is $22.45
Minimum sales was on Wednesday which is $3.78
Total weekly sales were $47.39
Average of the sales is $9.48
Sales too low for commission must earn more than $100

谢谢!

guomaochh 回答:使用循环,元组,列表,字典等的销售计算器

print ("Sales Calculator Program")

print ('\n')


expenses = []
for day_number in range (1,5 + 1):
    while True:
        user_input = float(input(f"Enter sales for day {day_number}\n> "))
        if user_input >= 0:
            expenses.append((day_number,user_input))
            break
        else:
            print(f"Amount may not be negative. Try again:")


print ('\n')       
max = max(expenses,key=lambda x: x[1])
min = min(expenses,key=lambda x: x[1])
total = sum(map(lambda x: int(x[1]),expenses))    
average = total/len(expenses)


for item in expenses :
    if item[0] == 1:
       Monday ...

对于其余的部分,您现在应该自己尝试一下,您可以使用max min sum等...在您询问此处之前,请先尝试了解一些内置函数:))

,

您可以这样做。

print ("Sales Calculator Program")

print ('\n')

expenses = []
for day in range(1,6):
    while True:
        sales = float(input(f"Enter sales for day {day}\n> "))
        if sales >= 0:
            expenses.append((sales,day))
            break
        else:
            print(f"Amount may not be negative. Try again:")

print ('\n')       

days = ['','Monday','Tuesday','Wednesday','Thursday','Friday']

maxval = max(expenses,key=lambda x: x[0]) # could also use operator.itemgetter
minval = min(expenses,key=lambda x: x[0])

total = sum(e[0] for e in expenses)
average = total / len(expenses)

print('Maximum sales was on %s which is $%.2f' % (days[maxval[1]],maxval[0]))
print('Minimum sales was on %s which is $%.2f' % (days[minval[1]],minval[0]))

print ("Total weekly sales were $%.2f" % total)
print ("Average of the sales is $%.2f" % (average))
if total < 100.0:
    print('Sales too low for commission must earn more than $100')

将具有日索引的每日值存储在元组中。然后,您可以将maxmin与键功能一起使用,以选择元素0作为用于计算最大/最小的元素。当您进行打印时,可以使用元组中的第二个元素索引到days数组中以获取日期名称。

平均数的计算应该很容易解释。

请注意,在任何需要正确的应用程序中,浮点数都不适合存储货币值。而是以分/便士存储值或使用decimal

本文链接:https://www.f2er.com/3169371.html

大家都在问