gpt4 book ai didi

python - 使用列表似乎无法将工作日与数字相匹配

转载 作者:行者123 更新时间:2023-11-28 22:08:19 25 4
gpt4 key购买 nike

我正在尝试使用列表将星期几与数字相匹配,但我不确定该怎么做。例如我想让它知道第 1 天是星期一,第 2 天是星期二,第 3 天是星期三,第 4 天是星期四,第 5 天是星期五

我已经用列表尝试了很多东西,但我似乎无法弄清楚

这就是我试图让它看起来像的东西:

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))

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

谢谢

最佳答案

为确保我正确理解问题 - 你有两个列表:

  • [10.22, 4.12, 3.78, 6.82, 22.45]
  • ['星期一'、'星期二'、'星期三'、'星期四'、'星期五']

并且您想将第一个列表中的数字与第二个列表中的天数相匹配。

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

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

然后你的 print() 可以是:

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

关于python - 使用列表似乎无法将工作日与数字相匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58687289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com