简体   繁体   English

在这个 Python 问题上遇到一些麻烦

[英]Having some trouble on this Python problem

I'm tasked with this question:我的任务是这个问题:

Design a program that asks the user to enter a store's sales for each day of the week.设计一个程序,要求用户输入商店在一周中每一天的销售额。 The amounts should be stored in a list.金额应存储在列表中。 Use a loop to calculate the total sales for the week and display the result.使用循环计算一周的总销售额并显示结果。

I've written this thus far到目前为止我已经写了这个

listf=[0,]
def total_list(listf):
sales_total= int(input("enter weekly sales:"))

for x in range(len(listf)):
    sales_total +=listf[x]
return sales_total

You're asking the user for the entire week's sales:您要求用户提供整周的销售额:

sales_total= int(input("enter weekly sales:"))

but the spec says that you should be asking them for the sales for each day of the week (individually) and put them in a list:但是规范说你应该向他们询问一周中每一天的销售额(单独)并将它们放在一个列表中:

asks the user to enter a store's sales for each day of the week.要求用户输入商店在一周中每一天的销售额。 The amounts should be stored in a list.金额应存储在列表中。

That might look something like:这可能看起来像:

>>> sales = [int(input(f"Enter the sales for {day}: ")) for day in "SMTWTFS"]
Enter the sales for S: 0
Enter the sales for M: 4
Enter the sales for T: 5
Enter the sales for W: 3
Enter the sales for T: 5
Enter the sales for F: 9
Enter the sales for S: 1

Now you have all the daily sales in a list:现在您将所有每日销售额都列在一个列表中:

>>> sales
[0, 4, 5, 3, 5, 9, 1]

and you can sum them to get the weekly sales:您可以将它们相加以获得每周销售额:

>>> sum(sales)
27

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM