简体   繁体   English

在 for 循环中分配变量

[英]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.

相关问题 将for循环的输出分配给变量 - Assigning the output of a for loop to a variable 在for循环中为变量赋值 - Assigning value to variable inside for loop 在while循环中变量未在python中分配值 - Variable not assigning value in python in while loop 在递归循环期间分配给 TensorFlow 变量 - Assigning to a TensorFlow variable during a recursive loop Python:使用循环语句将图像分配给变量 - Python : Assigning image to a variable using loop statement 在while循环中使用raw_input并将其分配给变量? - using raw_input in a while loop and assigning it to a variable? Python:为什么这段代码失败了? 将变量分配给for循环中的自身列表 - Python: why does this code fail? assigning variable to list of itself in for loop 通过使用 pandas 在循环中比较列和变量来为列分配值 - Assigning values to the column by comparing a column and a variable in loop using pandas Python:将列表中的每个项目创建为变量,并在 for 循环中为其赋值 - Python: Creating each item in a list as a variable and assigning a value to it in a for loop Python 是否在每个循环中为变量创建/分配值比在循环外创建/分配变量需要更多的内存/时间? - Python Does creating/assigning a value to a variable every loop take more memory/time than creating/assigning a variable outside the loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM