简体   繁体   中英

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM