Return an input that is inside a function

Asked

Viewed 1,399 times

-1

I have that code:

bebidas = {'Suco de laranja': 5.99, 'Suco de uva': 5.99, 'Suco de açaí': 5.99, 'Coca Cola(lata)': 6.50, 'Vitamina': 7.50}
salgados = {'Coxinha': 5.00, 'Pastel': 7.50, 'Hamburguer': 7.50}
sobremesas = {'Sonho': 2.99,'Bolo de chocolate': 4.99,'Pudim': 6.00,'Salada de frutas': 5.49}
lista = [1,2]
def menu():
ver = input('Você deseja ver o nosso menu?[Sim/Não]')
return ver
menu()

But I don’t know how to return the input to use the if structure on it.

  • You are already returning.

  • I reversed the issue because the new code doesn’t make much sense and misrepresents the answer, could it explain better what you don’t understand yet? Apparently the use of IF you have already understood, or not?

  • First I did the editing and then I went to explain. I’m trying to make a loop , but the input is what’s not being recognized and I think that’s why the loop doesn’t end. The problem is how to make this input that is inside the function be recognized. Only for this new code: def ver(): veja = input('Do you want to see our menu?[Yes/No]') while True: ver() if veja == 'Yes'. Lower(): break

  • I still don’t know how to add the code the right way in the comment.

  • IF has to go inside While, indented ... but I don’t understand why you put While, do as I did in the last example, use "recursion" in your def.

  • The ver() be set in veja, it should be like this ... True: veja = ver() if veja == ...

  • I used the last example and it worked, but I could not make the loop until the user type yes

  • Could explain better what you want, can’t understand what use you want with this loop

  • @Guilhermenascimento I want to loop this question from the menu until you type yes

Show 4 more comments

1 answer

3

It would be something like:

if menu() == "Sim":
    #chama o menu
else:
    #fecha o programa ou outra

Yet that would not be case-sensitve, then you can use .lower(), because if it is YES, yes, yes, Sim, yes, Sim and etc will work, example:

def menu():
    ver = input('Você deseja ver o nosso menu?[Sim/Não]')
    return ver.lower()

if menu() == "sim":
    print("Escreveu sim")
else:
    print("Escreveu qualquer outra coisa")

To facilitate you can also use only S and N (the N is relative, actually anything other than S and SIM would be no), for this use the in:

def menu():
    ver = input('Você deseja ver o nosso menu? Para confirmar digite Sim ou S ou Yes ou Y: ')
    return ver.lower()

if menu() in [ "sim", "s", "yes", "y" ]:
    print("Escreveu sim")
else:
    print("Escreveu qualquer outra coisa")

If the input is used for other "commands" you will create just using the elif (python has no switch/case structure) and save the value of input() in a variable, so for example:

def meuApp():
    resposta = input('Digite o seu comando: ').lower()

    if resposta == "menu":
        print(
            """1 - Digite "menu" (sem aspas) para abrir o menu novamente\n"""
            """2 - Digite "produtos" (sem aspas) para ver os produtos\n"""
            """3 - Digite "fechar" (sem aspas) para encerrar o programa\n"""
        )
    elif resposta == "produtos":
        print("\n\n\nExibindo os produtos\n\n\n")
    elif resposta == "fechar":
        exit()
    else:
        print("Invalido")

    meuApp() # deixa o app recursivo

meuApp() #inicia

Browser other questions tagged

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