'Subtracting from inner list
There is 5 fundraising projects, and the purpose of this program is to substract the amount of money that each of n donators will donate from the desired sum of each project
The program partially work, but sometimes python typing "TypeError: 'NoneType' object is not subscriptable" and the programm fall
What can cause this crash?
Thank you for your help!
n_month = 5
nl = [[], [],[], [], []]
place = 1
var = 1
#creating list of lists of the format [sum, number of project].
# for example this one: [[53, 1], [78, 2], [152, 3], [51, 4], [39, 5]]
for lst in nl:
summ = int(input("Please input miminum budget for next project:"))
lst.append(summ)
lst.insert(place, var)
place +=2
var+=1
n = int(input("Please enter number of donors: "))
for i in range(n):
project_n = int(input("Please enter project number (1-5): "))
donation = int(input("Please enter donation: "))
nl[project_n-1][0] -= donation
print(nl)
Solution 1:[1]
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
Solution 2:[2]
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
Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | C.Nivs |
| Solution 2 | Tom McLean |
| Solution 3 | Maddie Cope |

