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

xiaoxingxing python 206

原文标题Having some trouble on this Python problem

我的任务是处理这个问题:

设计一个程序,要求用户输入一周中每一天的商店销售额。金额应存储在列表中。使用循环计算一周的总销售额并显示结果。

到目前为止我已经写了这个

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

原文链接:https://stackoverflow.com//questions/71476341/having-some-trouble-on-this-python-problem

回复

我来回复
  • Samwise的头像
    Samwise 评论

    您正在向用户询问整周的销售额:

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

    但是规范说你应该向他们询问一周中每一天(单独)的销售额并将它们放在一个列表中:

    要求用户输入一周中每一天的商店销售额。金额应存储在列表中。

    这可能看起来像:

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

    现在,您在列表中列出了所有每日销售额:

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

    您可以将它们相加以获得每周销售额:

    >>> sum(sales)
    27
    
    2年前 0条评论