[英]Assigning a variable in a for loop
So i am writing a program for practice and needed some help with a for loop, The program takes the amount of people buying, the amount being brought and then the total money being paid.所以我正在编写一个用于练习的程序,需要一些 for 循环的帮助,该程序需要购买的人数,带来的金额,然后是支付的总金额。
Once it takes this it outputs the price per item一旦它接受它,它就会输出每件商品的价格
What i would like to happen next is for the program to use the amount of people stored in "GroupNo" to output the question "How much money has Person 1 put in, How much has person 2 put in"........ etc until it gets to the amount the user inputed.接下来我希望程序使用存储在“GroupNo”中的人数到 output 问题“第 1 人投入了多少钱,第 2 人投入了多少钱”...... ..等,直到达到用户输入的数量。
I know to get the loop to output the question the right amount of times, its a simple case of "for i in range (0, GroupNo)" but how do i get each answer to this question to be stored in a new variable each time it is asked我知道要循环到 output 这个问题的次数是正确的,它是“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()
One way could be to use a dictionary with the iteration as the key一种方法是使用以迭代为键的字典
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.