Printing lists

Asked

Viewed 53 times

-3

Could someone explain why the list of the following code is not printed? Thanks!

def produtos_pedidos(produtos):
    lista = []
    while True:
        pedido = input("Qual produto você quer pedir? Para encerrar o pedido digite 0: ")
        if pedido in produtos:
            quantidade = input("Qual a quantidade de {}? ".format(pedido))
            total = print("Você pediu {} {}. ".format (quantidade, pedido))
            lista.append(total)
        elif pedido == "0":
            print("Seu pedido foi encerrado")
            return lista
        else:
            input("Não temos esse produto. Para ver o cardápio digite C: ").upper()
            print(produtos)
  • Why have a parameter pedido if it is overwritten right on the first line of the loop? The same goes for the parameter quantidade.

2 answers

2

Instead of return lista, puts print(lista).

The return returns the value for another variable and does not print the result on the screen.

1


Your list is being returned to the main function, not printed, so in the main code you should have something like:

sua_lista = produtos_pedidos(produtos, pedido, quantidade)

Then the list that was only inside the function, will be passed to the main code. If you just want to show the list on the screen, and you don’t want to use it in the rest of the code, replace the return lista by a print(lista).

Edit:

You are passing to the variable total the return of print, but this function does not return. If in your list you want to store the product and the amount that was ordered, you should do something like that within that first if:

quantidade = input("Qual a quantidade de {}? ".format(pedido))
print("Você pediu {} {}. ".format (quantidade, pedido))
lista.append([pedido,quantidade])

You don’t even need the variable total just pass the values directly to the list.

  • with print(list) it shows no value at all

  • Now that I’ve noticed something, your total gets the print, but the print function returns nothing to store in the total variable, and it’s exactly the total that you pass to the list. What exactly should there be in this list? A value? A pair with the product name and quantity? Either way you need to pass the desired values to the total variable (or put them directly in the list) and print("you asked...), separately, without assigning to a variable.

  • I made an Edit in my answer, maybe now it works.

  • Thank you very much. I didn’t know I could put more than one argument in . append :)

  • But this append has only one argument, one list, and within that list has two values. Note that this way you will pass several lists into the 'list' variable, each of these smaller lists will have a pair, containing the product and the order quantity.

  • Eita. ok. I will continue studying.

  • Note also that, as Augusto Vasques suggested in his comment, you do not need to pass 'request' and 'quantity' as function parameters, because you do not use these values passed, you ask directly within the function. The only parameter you need is the 'products''.

Show 2 more comments

Browser other questions tagged

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