python非常新,但我想存储每次循环再次运行代码时生成的每个列表,以便在代码的后面部分使用它们
i = 1
while i <= 5:
units = input("Insert Units Sold: ")
rev = input("Insert Revenue of Sale: ")
revper = round((int(rev) / int(units)))
print("Revenue per unit: " + str(revper))
cogs = (3 * int(units))
print("Cost of Goods Sold: " + str(cogs))
profit = (int(rev) - (cogs))
print("Profit: " + str(profit))
profitper = round((profit / int(units)))
print("Profit per Unit: " + str(profitper))
L1 = [int(units), int(rev), revper, cogs, profit, profitper]
print("Line " + str(i) + str(L1))
i = i + 1
下面是一种方法:
然后,您可以在
myLists
上循环以使用for
循环获取每个生成的列表:变量
L1
将在每次循环迭代时被覆盖。但最后,最后一个值将保留在L1
中。如果这是你想要的,那么你已经拥有了如果要存储所有
L1
值,请将它们存储在另一个列表中:当然,对于第三个实例,您可以使用实例
lst[2]
访问每个迭代顺便说一下,在编码惯例中,变量不能使用大写字母。这些是为类和常量保留的。因此,最好使用
l1
而不是L1
希望它对您有所帮助,并帮助您提高python技能;-)
我会使用一组格言:
相关问题 更多 >
编程相关推荐