How to repeat the code at the end in python?

Asked

Viewed 7,177 times

3

so I’m starting programming (just by apender even ) and wanted to know how to make the code restart at the end.

I know it may seem simple but I don’t do any courses, I’m learning by myself

lanchonete = ("500$")

açaiteria = ("900$")

uber = ("200$")

pationorte = ("1200$")

pergunta = input ("Diga o nome do estabelecimento desejado ")

if pergunta ==("lanchonete"):
   print (lanchonete)

elif pergunta ==("açaiteria"):
   print (açaiteria)

elif pergunta ==("uber"):
   print (uber)

elif pergunta ==("pationorte"):
   print (pationorte)

else:
   print ("Este estabelecimento não esta cadastrado no nosso banco de 
   dados")

2 answers

6

Since you’re learning, it’s worth commenting on. What you did in your code was basically relate four variables to their respective values and, at first, they will not vary during execution. Something like this is usually called a map: you define a direct relationship between a key and a value. In this case, the key would be the name of the establishment and the value the related monetary quantity. In Python, the simplest way to implement this is with a dictionary:

estabelecimentos = {
    'lanchonete': '500$',
    'açaiteria': '900$',
    'uber': '200$',
    'pationorte': '1200$'
}

To get the value from a key, just use the method get, that even allows you to set a default value in case the key does not exist:

padrão = 'Este estabelecimento não esta cadastrado no nosso banco de dados'
pergunta = input("Diga o nome do estabelecimento desejado ")
print(estabelecimentos.get(pergunta, padrão))

Putting this inside a loop of repetition, it would be:

estabelecimentos = {
    'lanchonete': '500$',
    'açaiteria': '900$',
    'uber': '200$',
    'pationorte': '1200$'
}

padrão = 'Este estabelecimento não esta cadastrado no nosso banco de dados'

while True:
    pergunta = input("Diga o nome do estabelecimento desejado ")
    print(estabelecimentos.get(pergunta, padrão))

See working on Repl.it

1

Just wrap your code in a loop while True.

The while checks a condition, and if it is true, runs again everything that is inside your indentation block. How we pass True, he will always run everything again.

As another hint: parentheses are unnecessary in assignments (=) and comparison (==), and are only needed here for functions. Your code becomes a little clearer without them!

while True:
    lanchonete = "500$"

    açaiteria = "900$"

    uber = "200$"

    pationorte = "1200$"

    pergunta = input ("Diga o nome do estabelecimento desejado ")

    if pergunta == "lanchonete":
       print (lanchonete)

    elif pergunta == "açaiteria":
       print (açaiteria)

    elif pergunta == "uber":
       print (uber)

    elif pergunta == "pationorte":
       print pationorte

    else:
       print ("Este estabelecimento não esta cadastrado no nosso banco de dados")

Browser other questions tagged

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