在Python中,如何在每次循环时创建一个新变量,然后将它们全部发送到要处理的函数?

在Python中,如何在每次循环时创建一个新变量,然后将它们全部发送到要处理的函数?,python,function,loops,module,Python,Function,Loops,Module,我对python还比较陌生,这可能是我力所不及的,但我正在编写一个程序(针对学校编程课程),该程序要求提供一个新项目、该项目的价格以及每次循环时该项目的数量。(当程序询问是否有更多项目需要处理时,通过键入stop停止循环)然后程序将所有这些变量发送到我创建的模块,该模块将根据数量计算每个项目的总成本,然后打印出每个项目的成本和总成本 我的老师说这是可能的,但他不太知道怎么做 以下是我到目前为止所做的(我尝试使用列表,但似乎没有成功): 第一个程序(total_run.py): 第二个计划(总成本

我对python还比较陌生,这可能是我力所不及的,但我正在编写一个程序(针对学校编程课程),该程序要求提供一个新项目、该项目的价格以及每次循环时该项目的数量。(当程序询问是否有更多项目需要处理时,通过键入stop停止循环)然后程序将所有这些变量发送到我创建的模块,该模块将根据数量计算每个项目的总成本,然后打印出每个项目的成本和总成本

我的老师说这是可能的,但他不太知道怎么做

以下是我到目前为止所做的(我尝试使用列表,但似乎没有成功):

第一个程序(total_run.py):

第二个计划(总成本):


你的数据结构是错误的。实际上,您只需要一个项目列表,该列表中的每个项目本身就是名称、价格和数量的列表(或元组)。然后,您可以简单地将列表中每个项目的价格相加

items = []
while stop != "stop":
    name = input("What is the name of the item?\n")
    price = float(input("What is the price of the item?\n"))
    quantity = float(input("How many of these items are there?\n"))
    items.append((name, price, quantity))
现在,您可以轻松计算每个项目的总金额,以及总金额:

overall_total = 0
for item in items:
    total_price = item[1] * item[2]
    overall_total += total_price

您可以单独保存列表,然后使用
for
循环遍历它们,如下所示:

costs = []
while stop != "stop":
    itemname.append(input("What is the name of the item?\n"))
    itemprice.append(float(input("What is the price of the item?\n")))
    quantity.append(float(input("How many of these items are there?\n")))
    stop = input("Enter \"stop\" to add up the total")

for i in range(len(itemname)):
    costs.append(itemprice[i]*quantity[i]) 

total_cost = sum(costs)
值得注意的事情(无法评论,因此请在此部分回答,抱歉):

  • 在total_cost.total中,您定义itemprice=0,然后乘以它
    itemprice*=values
    使itemprice保持为0
  • 将itemprice和itemname传递给total_cost.total函数,然后重新定义itemprice=0并将itemname重新分配给自身(但不会破坏代码)
  • python中的字符串可以通过
    来定义,而另一个字符串不会停止该字符串。因此
    my_string=“您好,这是一个美好的一天”
    stop=input('Enter“stop”来合计价格)
    将起作用
  • 您可以通过使用
    for
    循环(如上所示)遍历列表来打印未知数量的项目
  • 干杯

    因为,最好将一个物品的数据打包到一个生物中。
    他建议
    tuple
    ,我建议
    class

    class Item:
        def __init__(self, name=None, price=None, quantity=None):
            self.name, self.price, self.quantity = name, price, quantity
        @classmethod
        def ask_user(cls):
            return cls(input('The name of Item: '), float(input('The price: ')), float(input('How much of Items you have: ')))
        def value(self):
            return self.price * self.quantinity
    
    def total_value(items):
        sum = 0
        for i in items:
            sum += i.value()
        return sum
    
    另外,您可以等待文件结束,而不是等待
    “停止”
    (Linux:Ctrl+D在空行中,Windows:Ctrl+Z,然后输入):


    哇,这太简单了,谢谢丹尼尔!不过有一个简单的问题。我如何从列表中获取名称并将其与总价一起打印?@1adog1您可以使用字典存储每个项目的总成本-
    {item\u name:totalcost,item\u name 2:totalcost,…}
    甚至
    以zip格式存储名称、价格、数量(项目名称、项目价格、数量):
    costs = []
    while stop != "stop":
        itemname.append(input("What is the name of the item?\n"))
        itemprice.append(float(input("What is the price of the item?\n")))
        quantity.append(float(input("How many of these items are there?\n")))
        stop = input("Enter \"stop\" to add up the total")
    
    for i in range(len(itemname)):
        costs.append(itemprice[i]*quantity[i]) 
    
    total_cost = sum(costs)
    
    class Item:
        def __init__(self, name=None, price=None, quantity=None):
            self.name, self.price, self.quantity = name, price, quantity
        @classmethod
        def ask_user(cls):
            return cls(input('The name of Item: '), float(input('The price: ')), float(input('How much of Items you have: ')))
        def value(self):
            return self.price * self.quantinity
    
    def total_value(items):
        sum = 0
        for i in items:
            sum += i.value()
        return sum
    
    items = []
    
    while True:
        try:
            items.append(Item.ask_user())
        except EOFError: #no mire items to read
            break #exit from the loop
        except Exception as e: #other error
            print(type(e), e)  #print its type and the message
    
    print(total_value(items))