How to search within Python code

Asked

Viewed 183 times

0

Hello, I would like to make a code that when I typed for example, "product name", it checked if there is any word with the name I typed and returned a value, example "200 real". But I have no idea how to start, I’d be grateful if someone could help me!

  • The @Antonygabriel response gives the backing for your need ; take a look with care, study about Python dictionaries, then you’ll be prepared for it

1 answer

5


You can use a dictionary to do what you want, see:

v = {
     'Celular':"200",
     "Bolsa":"200", 
     "Garrafa":"2"
     }                                    # Os valores também podem ser inteiros
r = input("Digite o valor do produto: ")  # Desde que aqui também seja

print("Por esse preço nós possuimos: \n")
for i in v.keys():
    if r == v[i]:
        print(i)

exit:

>>> Digite o valor do produto: 200
>>> Por esse preço nós possuimos: 
>>> 
>>> Celular
>>> Bolsa
  • 1

    I think he wanted the opposite ; search by name and get the value

  • 1

    But you can do it this way too, just reverse, in place of the product puts the value and in place of the value puts the product.

  • 1

    I know, that’s why I commented on the question that your answer was beautiful ;-)

  • I wanted the opposite, yes, but I did what @Antony Gabriel said, reversed. Just a question, would be like after returning the value, ask to enter another name?

  • Yes, you can use one while for that reason.

Browser other questions tagged

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