Optimizing Data Output

Asked

Viewed 42 times

0

Good morning:

I made this code for an exercise. My question is: How can I optimize the outputs without having to do multiple prints. In this code I made for three exits. Only if it was for 10 exits I would have to do 10 prints.Another question I have. In the text "the value of the commission of the FIRST representative..."...this term...first...second...third...has some way I leave it automated without I have to type one by one. Below follows my code.

itens = []
item=0
valor_item = 50

for i in range(3):
    item = int(input("\nQuantidade de itens do %dº representante: " % (i+1)))
    itens.append(item)


comissoes = []
for item in itens:
    comissao = (0.1*valor_item*item) if item <= 19 else \
               (0.15*valor_item*item) if item >=20 and item<= 49 else \
               (0.20*valor_item*item) if item >=50 and item <=74 else \
               (0.25*valor_item*item) 
    comissoes.append(comissao)


print("\nValor da Comissão do do Primeiro Representante: R$ %5.2f% " %comissoes[0])
print("\nValor da Comissão do do Segundo Representante: R$ %5.2f% " %comissoes[1])
print("\nValor da Comissão do do Terceiro Representante: R$ %5.2f% " %comissoes[2])

Grateful for the attention

1 answer

0


Use a bow tie for, just like you did to put together the list comissoes.

for indice, comissao in enumerate(comissoes, start=1):
    print("\nValor da Comissão do %dº Representante: R$ %5.2f" % (indice, comissao))

Docs to the function enumerate: https://docs.python.org/3/library/functions.html#enumerate


As for your code as a whole, you can do everything with a single loop and without using lists.

quantidade_representantes = 3
valor_item = 50

for i in range(1, quantidade_representantes + 1):
    item = int(input("\nQuantidade de itens do %dº representante: " % i))

    if item <= 19:
        multiplicador_comissao = 0.10
    elif item <= 49:
        multiplicador_comissao = 0.15
    elif item <= 74:
        multiplicador_comissao = 0.20
    else:
        multiplicador_comissao = 0.25

    comissao = multiplicador_comissao * valor_item * item

    print("Comissão do %dº representante: R$ %5.2f" % (i, comissao))
    print()

It’s just a hint.

  • Gosh. Thank you. You helped a lot.

Browser other questions tagged

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