在for循环中赋值变量



所以我正在写一个练习程序,需要一些for循环的帮助,该程序需要购买的人数,购买的数量,然后支付的总金额。

一旦它接受这个,它输出每件商品的价格

接下来我想让程序使用存储在"GroupNo"中的人数。输出问题"第一人投入了多少钱,第二个人投入了多少钱"........等等,直到用户输入的金额。

我知道要让循环输出问题的正确次数,这是一个简单的"for I in range (0, GroupNo)"&;但是我如何得到这个问题的每个答案被存储在一个新的变量,每次它被要求

GroupNo = int(input("How many people are buying?"))
Gs = int(input("How many are you picking up?"))
Ps = int(input("How much money is being paid?"))
PPG = Ps / Gs
print ("Price per item is:" ,"£",PPG,)
def MainCalc():
for i in range (0, GroupNo):
int(input("How Much is person 1 paying:")) #This would increment up till it hits GroupNo
MainCalc()

一种方法是使用字典,将迭代作为键

def main_calc():
payments = dict()
for i in range(1, GroupNo + 1):
value = input(f"How Much is person {i} paying: ")
payments[i] = int(value)
return payments

# example code
data = main_calc()
print(data) # view entire dictionary
print(data[1]) # view only person 1

最新更新