2
My code is having some small problems, it receives "institutions" (names that the user wants in the input) inside a dictionary this input is passing pro dicionario as a list, and then I use the list to make a "donation" until ai ta tranquil the problem happens when it is in a loop of repetition doing that process more than once in the part of the "institution" works normally without overwriting the previous but when and in the donation the last value added and subsisted by the current ex: was donated 1000 and in the second donation was donated 200 more should keep the value 1200, however the value 1000 add up and is only the last value 200, here the code, if you can help as soon as possible.
instituicoes = []
def doação():
while True:
instituicao_add = input("0 para deixar de adicionar\ndigite alguma instituicao\n:") #ex: 'stack overflow pt'
if(instituicao_add == '0'):
break
instituicoes.append({'nome': instituicao_add})
while True:
for i, xc in enumerate(instituicoes):
print(i,"-",xc)
x = int(input("\nQual instituição deseja escolher?")) # ex: 0
while x >= len(instituicoes) or x < 0: # Verificar se index existe na nossa lista de instituicoes
x = int(input("\nQual instituição deseja escolher?")) # ex: 0
dinheiro = int(input("\nQuanto deseja doar?\n"))
instituicoes[x]['doação'] = dinheiro # ex: 1000
print("Obrigado por ajudar esta instituição")
print(instituicoes[x]) # output: {'nome':'stack overflow pt', 'doação': 1000}
#para imprimir só o valor doado
print(instituicoes[x]['doação'])
doação()
Right the code and basically the one you had fixed before with small changes but it’s bullshit, I’ll test and pass you the feedback.
– Alysson Maia
That’s right, man, VLW you’re the 10.
– Alysson Maia
Thank you. I’m glad I helped @Alysson
– Miguel
only the level of curiosity would have to add up the final result if I had let’s suppose 4 or more institutions in the case all collected ?
– Alysson Maia
The total of all donations? In the end you can even do it in two lines:
doacoes = [i['doação'] for i in instituicoes]
and in the line below cantotal = sum(doacoes)
. This way is called list compreension. You could achieve the same only with cycle goes– Miguel