Variable creation via user response in Python

0

I would like to have the user enter a response of that response would create a "variable" or not. I do not know if creating a new variable would be the best way, for example:

a = str(input('Existe mais produtos para serem adicionados?: '))

In this part the user would answer yes or no , if the answer were yes , the program would create another variable that would receive a certain value float (a2 a3 a4) and so on.

    
asked by anonymous 20.03.2018 / 01:57

2 answers

0

I recommend that you use dictionaries. This way you can track the value by the name of the item, for example. If you wanted to loop a while until there were no more products to add, just use the following code.

 #criamos nosso dicionario
d={}
a = "s"
#utilizei o lower pra sempre deixar a resposta em minusculo, pra nao ter problemas com input maiusculo
while a.lower() == "s":
    item = input("Digite o item que deseja adicionar")
    valor = float(input ("Digite o valor do item %s: "%item))
    d[item] = valor
    a = input ("Deseja adicionar mais algum item? s/n")
#para exibir em for, utilizamos a seguinte sintaxe
for x in d:
    #como o x vai rodar sempre com o valor da chave (nome), podemos chamar o valor do preço com a o dicionario[x] (d[x])
    print ("Item: %s \tPreço: %.2f"%(x, d[x]))
    
20.03.2018 / 02:32
1

The user create is not possible. But here I have a solution

a= str (input ('Existe mais produtos para serem adicionados?: '))
a2 = 0
a3 = 0
a4 = 0
if a == 'sim':
    a2 = 1
else:
    a3 = 1

But, I guess the best thing in your code would be a list. Something in that genre:

lista = []
a= str (input ('Existe mais produtos para serem adicionados?: '))
if a == 'sim':
   lista.append(1) #valor
So, if you put in a while, changing what you put inside the append, and changing if, you can make an infinite list, which I think is what you want, hence you run the while, while the answer is different from 'no'. If you need help with how to do it, leave the code, or specify what you want, I'll be happy to help.

    
20.03.2018 / 02:03