处理列表似乎无法使工作日与数字匹配

时间:2019-11-04 04:41:27

标签: python

我正在尝试使用列表将星期几与数字匹配,但我不确定如何匹配。例如,我想让它知道第一天是星期一,第二天是星期二,第三天是星期三,第四天是星期四,第五天是星期五

我已经尝试过使用列表进行多项操作,但似乎无法弄清楚

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

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

这是我到目前为止所拥有的:

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:")
days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')




print ('\n')       

finalExpenses = sum(expenses)
average = finalExpenses / 5
maxExpenses = max(expenses)
minExpenses = min(expenses)

if finalExpenses < 100:
    print( "Sales too low for commission must earn more than 100$")
elif finalExpenses < 250:
    print ( "You get a commission of 25$")
elif finalExpenses < 500:
    print ( "You get a commission of 30$")
else:
    print ( "You get a commission of 40$")




print ("Maximum sales on is $" +str(maxExpenses))
print ("Minimum sales is $" +str(minExpenses))
print ("Total weekly sales were $" +str(finalExpenses))
print ("Average of the sales is $" +str(average))

除了将日期与数字相匹配并按照示例所示进行打印外,我还完成了所有工作。

谢谢

2 个答案:

答案 0 :(得分:3)

为确保我正确理解问题,您有两个列表:

  • [10.22, 4.12, 3.78, 6.82, 22.45]
  • ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']

,您希望将第一个列表中的数字与第二个列表中的日期进行匹配。

您想要的是list.index()方法:

maxExpenses = max(expenses)
index = expenses.index(maxExpenses)
day = days[index]

,然后您的print()可以是:

print ("Maximum sales on ' + day + ' is $" +str(maxExpenses))

答案 1 :(得分:1)

您可以从index模块使用argmaxargmin函数:

numpy

输出:

import numpy as np
finalExpenses = sum(expenses)
average = finalExpenses / 5
maxExpensesDay = np.argmax(expenses)
minExpensesDay = np.argmin(expenses)

if finalExpenses < 100:
    print( "Sales too low for commission must earn more than 100$")
elif finalExpenses < 250:
    print ( "You get a commission of 25$")
elif finalExpenses < 500:
    print ( "You get a commission of 30$")
else:
    print ( "You get a commission of 40$")

print ("Maximum sales on " + days[maxExpensesDay] + " is $" +str(expenses[maxExpensesDay]))
print ("Minimum sales on " + days[minExpensesDay] + " is $" +str(expenses[minExpensesDay]))
print ("Total weekly sales were $" +str(finalExpenses))
print ("Average of the sales is $" +str(average))