Skip to content
Advertisement

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!

enter image description here

    
    
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)   

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement