There is 2 fundraising projects, and the purpose of this program is to substract the amount of money that each donator will donate from the desired sum of each project
the last line of my code should do that. I understand that it doesn't save the changes through the iteration because the result of the line is not assigned to any value.
I wonder how I assign this change to the original list?
I mean if all 3 donators will donate 150 $ to project 1, the result should be: nl = [[350, 1], [400, 2]]
Thank you for your help!
nl = [[800, 1], [400, 2]] # each inner list is a fundraising project. the first number in each inner loop refer to the desired amount of money, and the second number is the number of the project
for i in range(3): # there will be 3 donations, the donator will specify to which project and how much money he want to donate
project_n = int(input("Please enter project number (1-5): "))
donation = int(input("Please enter donation: "))
nl[project_n][0] - donation
CodePudding user response:
You need to reassign the value which you can do with the -=
notation:
nl = [[800, 1], [400, 2]] # each inner list is a fundraising project. the first number in each inner loop refer to the desired amount of money, and the second number is the number of the project
for i in range(3): # there will be 3 donations, the donator will specify to which project and how much money he want to donate
project_n = int(input("Please enter project number (1-5): "))
donation = int(input("Please enter donation: "))
nl[project_n][0] -= donation # here
CodePudding user response:
As already mentioned, you need to use the -=
operator. I would also recommend using a dictionary for this:
nl = {
1: {"money": 800},
2: {"money": 400}
}
for i in range(3): # there will be 3 donations, the donator will specify to which project and how much money he want to donate
project_n = int(input("Please enter project number (1-5): "))
donation = int(input("Please enter donation: "))
nl[project_n]["money"] -= donation
CodePudding user response:
nl = [[800, 1], [400, 2]] # each inner list is a fundraising project. the first number in each inner loop refer to the desired amount of money, and the second number is the number of the project
donate = True
while donate == True:
project_n = int(input("Please enter project number (1-5): "))
donation = int(input("Please enter donation: "))
nl[project_n][0] = nl[project_n][0] - donation
anotherdonation = input("Do you want to donate to another project (Yes or No): ")
if anotherdonation == "No":
donate = False
else :
project_n = int(input("Please enter project number (1-5): "))
donation = int(input("Please enter donation: "))
nl[project_n][0] = nl[project_n][0] - donation
anotherdonation = input("Do you want to donate to another project (Yes or No): ")
This is another way to approach this, in case the donor is only contributing to one fundraiser. Im not sure by your comments if it has to give to three. Also be careful python starts counting from 0, so either change your input to 0-4 or in your code make sure to subtract one.