Conversion of response to lists

Asked

Viewed 229 times

2

I am having a small problem in my algorithm I have to receive a "name of an institution" any typed by the user and convertlist to use it in future other conditions but it is converting to string all the letters of the name and putting in a list thus being unable to use it as a new list declared by the "user" to use in other conditions.

instituicão = []

    print("Digite o nome da instituição que deseja adicionar.\n 0 - Para 
    parar de adicionar\n")
    while True:
        instituicão_add = input("digite alguma instituicao")
        #Supor que o usuário botou "ADD"
        conversão_lista = list(instituicão_add)

        #funcionaria se tivesse em str mas o print vai sair como dito
        #ja  para o input direto nao sei qual condição bota para parar
        if instituicão_add == "0": #<<< ???  
            break
        else:
           instut.append(conv_list)
           print(instituicão)           
           #Ai no caso meu print sai se tiver str(input("")) assim [['a', 'd', 'd']]
           #O certo para min seria sair assim [["alguma coisa"]] 

for indice,na_lista in enumerate(instituicão):
    print(indice, "-", na_lista)

inst = int(input("\nQual instituição deseja escolher?"))

x = inst #recebendo o indice que seria pra entra na lista que desejasse

if inst == x:
         dinheiro = int(input("\nQuanto deseja doar?\n")
         instituicão[x].append(dinheiro)
         print("Obrigado por ajudar esta instituição")
         print(instituicão[x])

Here in this last "print" I wanted it to come out [["value of donated money"]], by the user within the institution added by it is transformed into a list that should return [["xxxxx"]], however I am not able to make this algorithm work so if you can help me.

  • 1

    Python3 lets you accentuate variables - but that doesn’t mean you should do it! : -) But if you’re going to do it, it’s important to maintain consistency: The variable should be instituição (or instituicao) - never instituicão - how will you be able to remember where you used special names or not when the program is larger?

  • Alias- try using the same names for the same variable - having a "conversion_list" that you try to use a few lines below with the name "conv_list" will not help your program to work.

  • hey how I keep the received value without overwriting the previous one because how and dicionario I am not getting saved as I do in normal lists, type if in the donation type 1000 and I want to donate again and type 20 should be saved 1020 and not overwrite for 20 and I have no idea how to do this in dictionary.

1 answer

2


Okay, I think I get it. You want to store all the institutions typed by the user in a list:

Why not make a list of dictionaries where each store the institution’s name and donation? If I have understood the problem correctly, in this context I think it would be best:

Python 3.x:

instituicoes = []
while True:
    instituicao_add = input("digite alguma instituicao") #ex: 'stack overflow pt'
    if(instituicao_add == 'exit'):
        break
    instituicoes.append({'nome': instituicao_add})

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'])
  • But in this case I do not have how to use the "institution" as list inside list to then "donate", for some of these institution just going through the list through the Dice to choose one that is stored, I tried before this and so on, but vlw still not sure by this fact because then I do not have how to use as a list informed by the user stored inside another list. And already Exit will finish all the execution I just need the condition in list mode to enter break.

  • But tell me something... institution is a list of strings or a list of lists? So I didn’t quite understand what you want to store inside that var.

  • Institution and the list that receives the lists with the names that the user informs the problem and that is being string and not list within the list type institution =[ ] ai when the user informs the name of the institution to be saved in the list institution = [informed by the user]] what type it would enter on behalf and the program make that name informed turns into a user-defined list without having to manually add each.

  • If I understand correctly then you want to add lists (of institutions). I will edit on top according to the solution that can solve this problem.

  • Exactly with what I have already learned and tried some things but you go ahead and can’t do that when the "name" typed by the user becomes a list to be stored inside another list

  • You can test that code just as hard as you are, if I understand correctly it solves your problem

  • Thank you very much is for the fact that I have not taken classes of dicionario yet but I understood perfectly thank you very much.

  • hey how I keep the received value without overwriting the previous one because how and dicionario I am not getting saved as I do in normal lists, type if in the donation type 1000 and I want to donate again and type 20 should be saved 1020 and not overwrite for 20 and I have no idea how to do this in dictionary.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.