Menu Cafeteria

Asked

Viewed 69 times

-4

Greetings, I am beginner in python and I need to make a code in which it starts with a table, stating the product, its value and a code for it. The program has to request the code of a product, the desired quantity and return the value times the quantity that was requested... I did several tests, in one he did not accept anything, in the other, no matter the values placed, the program returned me zero, and in my last attempt, the program requests infinitely the code and the amount, I thank


print('================================= \n| COD |     PRODUTO     | VALOR | \n| 100 | Cachorro quente | 1,20R$| \n'
      '| 101 | Bauru Simples   | 1,30R$|\n| 102 | Bauru com ovo   | 1,50R$|\n| 103 | Hamburguer      | 1,20R$| \n'
      '| 104 | Chessburguer    | 1,70R$|\n| 105 | Suco            | 2,20R$| \n| 106 | Refrigerante    | 1,00R$|\n'
      '=================================\n Para sair digite 999')
total = 0
while True:
    q = int(input('Informe o codigo: '))
    if(q == 999):
        break
    qtd = int(input('Informe a quantidade: '))
    if q == 100:
        total = 1.20 * q
    elif q == 101:
        total = 1.30 * q
    elif q == 102:
        total = 1.50 * q
    elif q == 103:
        total = 1.20 * q
    elif q == 104:
        total = 1.70 * q
    elif q == 105:
        total = 2.20 * q
    elif q == 106:
        total = 1 * q
    else:
        print('Codigo invalido')
print(total, 'reais') ``` 
  • makes a if q c in the variable q come a value for ex -1 vc da a break no while. so you can go to the final print

  • In all accounts, you’re multiplying by q (code) and should multiply by qtd (quantity). Also, make total += <sua conta aqui>. Other than that, the code looks OK.

2 answers

-2


Answering your question, your error is in the coding, where you are having the order value printed out of the while True: so never executing it, Another thing I noticed is that you are multiplying the value of the product by the code and not by the amount where I believe it would be right,

I made some changes to illustrate what I said I hope it helps you.

a = ('================================= \n| COD |     PRODUTO     | VALOR | \n| 100 | Cachorro quente | 1,20R$| \n'
  '| 101 | Bauru Simples   | 1,30R$|\n| 102 | Bauru com ovo   | 1,50R$|\n| 103 | Hamburguer      | 1,20R$| \n'
  '| 104 | Chessburguer    | 1,70R$|\n| 105 | Suco            | 2,20R$| \n| 106 | Refrigerante    | 1,00R$|\n'
  '=================================\n Para sair digite 999')
total = 0

while True:

    print(a)

   q = int(input('Informe o codigo: '))
    if(q == 999):
        break
    qtd = int(input('Informe a quantidade: '))
    if q == 100:
       total = 1.20 * qtd
    elif q == 101:
        total = 1.30 * qtd
    elif q == 102:
        total = 1.50 * qtd
    elif q == 103:
        total = 1.20 * qtd
    elif q == 104:
        total = 1.70 * qtd
    elif q == 105:
        total = 2.20 * qtd
    elif q == 106:
         total = 1 * qtd
    else:
        print('Codigo invalido')

    print('o Seu total foi ->', total, 'reais')

    b = input('deseja continuar: ')

    if b == 'sim':
       pass

    else:
       break

in this case I only changed the variable that was multiplying the value, I inserted the print within the loop of while True: and added a check to close the code if it is no longer used

-4

I used the dictionary, a data structure that is very useful and uses the key-value pair. I believe that working like this is more natural.

print('================================= \n| COD |     PRODUTO     | VALOR | \n| 100 | Cachorro quente | 1,20R$| \n'
      '| 101 | Bauru Simples   | 1,30R$|\n| 102 | Bauru com ovo   | 1,50R$|\n| 103 | Hamburguer      | 1,20R$| \n'
      '| 104 | Chessburguer    | 1,70R$|\n| 105 | Suco            | 2,20R$| \n| 106 | Refrigerante    | 1,00R$|\n'
      '=================================\n Para sair digite 0')

menu = {

    '100': ['Cachorro quente', 1.20],
    '101': ['Bauru Simples', 1.30],
    '102': ['Bauru com ovo', 1.50],
    '103': ['Hamburguer', 1.20],
    '104': ['Chessburguer', 1.70],
    '105': ['Suco', 2.20],
    '106': ['Refrigerante', 1.00]
}

pedido = {}
while True:
    codigo = input('Informe o codigo: ')
    if (codigo == '0'):
        break
    if codigo in menu:
        quantidade = int(input('Informe a quantidade: '))
        if quantidade > 0:
            valorItem = menu.get(codigo)
            pedido.update({codigo: (valorItem, quantidade)})

valorDoPedido = 0

for linha in pedido.items():
    valorDoPedido += linha[1][0][1] * linha[1][1]  # Preço * Quantidade

print('\nSEU PEDIDO: ')

for linha in pedido.items():
    print(str(linha[1][1]) + ' x ' + 'R$ ' + str(round(linha[1][0][1], 2)) + ' --- ' + str(linha[1][0][0]))

print('\nTOTAL DO PEDIDO: R$ ' + str(round(valorDoPedido, 2)))

Browser other questions tagged

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