How to Catch the Smallest Value of a Series of Inputs

Asked

Viewed 63 times

0

the user enters a code and the desired quantity, this code refers to some value of the list and then the value of the list is multiplied by the quantity. of this multiplication I want the most expensive value (already got) and the cheapest, this last one I could not do because to make 'cheap = 0' would already make the smallest value possible, I enclosed here.

codigos = [4.20, 7.30, 8.50, 9.20, 10.30, 8.30]
val_tot = frete = caro = 0
barato  = ?

while True:
menu = int(input('[1] Adicionar pedido \n[-1] finalizar programa: '))
if menu == -1:
    break
elif menu == 1:
 input('Digite o CPF: ')
 cod = int(input('Digite o código do produto: '))
 qtd = int(input(' Quatidade: '))
 frete += 5
 if cod == 100:
     val_tot += codigos[0] * qtd
     if codigos[0] * qtd > caro:
         caro = codigos[0] * qtd
     elif codigos[0] * qtd < barato:
         barato = (codigos[0] * qtd)
 elif cod == 101:
     val_tot += codigos[1] * qtd
     if codigos[1] * qtd > caro:
         caro = codigos[1] * qtd
     elif codigos[1] * qtd < barato:
         barato = (codigos[1] * qtd)
 elif cod == 102:
     val_tot += codigos[2] * qtd
     if codigos[2] * qtd > caro:
         caro = codigos[2] * qtd
     elif codigos[2] * qtd < barato:
         barato = (codigos[2] * qtd)
elif cod == 103:
    val_tot += codigos[3] * qtd
    if codigos[3] * qtd > caro:
        caro = codigos[3] * qtd
    elif codigos[3] * qtd < barato:
        barato = (codigos[3] * qtd)
elif cod == 104:
    val_tot += codigos[4] * qtd
    if codigos[4] * qtd > caro:
        caro = codigos[4] * qtd
    elif codigos[4] * qtd < barato:
        barato = (codigos[4] * qtd)
elif cod == 105:
    val_tot += codigos[5] * qtd
    if codigos[5] * qtd > caro:
        caro = codigos[5] * qtd
    elif codigos[5] * qtd < barato:
        barato = (codigos[5] * qtd)
print('Pedido mais caro: {}' .format(caro))
print('Pedido mais barato: {}' .format(barato))
print('Valor total arrecadado pelo motoboy: {}'.format(frete * 0.6))
print ('Valor arrecadado pela lanchonete: {}'.format(val_tot + (frete * 0.4)))
  • 2

    You can store in a variable the cheapest value and compare. If it is smaller you update this value.

  • Start with barato = 999999 ?

2 answers

1

I’d make a dictionary produto where the key is the code and the value is the price.

Every passage in the while i would add a pedido in a list whenever the user responds s.

When the answer is n the functions min and max return the order "cheap" and the most "expensive".

Note that no validation is done. Then type the correct codes to test otherwise it will give problem. Valid codes: 10, 20, 30, 40, 50 and 60

produto = {10:4.20, 20:7.30, 30:8.50, 40:9.20, 50:10.30, 60:8.30}

pedido = []

continuar = 's' 

while continuar == 's':    

    cod = int(input('Digite o código do produto: '))

    qtd = int(input(' Quatidade: '))

    pedido.append(produto.get(cod) * float(qtd))

    print(pedido)

    continuar = input("Deseja continuar? 's' ou 'n' ")

    if continuar == 'n':        
        print('\nPedido mais caro: {}' .format(max(pedido)))
        print('Pedido mais barato: {}' .format(min(pedido)))
        break

0

Start the variable barato with None

barato = None

And in all the lines where you check if the value is the cheapest, change the elif for if and add the test barato is None or ...

I mean, where was this:

elif codigos[0] * qtd < barato:
     barato = (codigos[0] * qtd)

Will turn this:

if barato is None or codigos[0] * qtd < barato:
    barato = (codigos[0] * qtd)

So on for all codes.

Browser other questions tagged

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