Program Creation in Python 3

1

I'm doing an exercise:

  

A factory has 10 representatives. Each receives a commission calculated from the number of items in an order, according to the following criteria:   - for up to 19 items sold, the commission is 10% of the total value of the order;   - for orders of 20 and 49 items, the commission is 15% of the total value of the order;   - for orders from 50 to 74 items, the commission is 20% of the total value of the order; and   - for orders equal or higher, at 75 items the commission is 25%.

     

Make a program that reads the order quantity of each representative and prints the commission percentage of each one.

I thought of creating a variable for each entry, ie a variable for each delegate and at the end to generate the commissions by putting several if elif and else. My question is to leave this code leaner without having to create the 10 variables?

More or less I made this piece of code that gives me the 10 entries. But from now on I curled up in time to create the conditionals to generate the value of each commission of each representative.

My code outline, with the entries:

print ("\n--------------------------------------------------------")
print ("\n--------------------------------------------------------")


def exercicio():    
    itens = []    
    item = 0

    while item == 0:
        item = float(input("\nQuantidade de Itens do 1º Representante: "))
        if item < 0:
            print("\nAs Quantidades não podem ser menores que Zero.")
            itens.append(item)


    for i in range(9):
        item = float(input("\nQuantidade de Itens do %dº Representante: " % (i+2)))
        itens.append(item)



   print("\n",item)
   print ("\n",itens)


exercicio()

print ("\n--------------------------------------------------------")
print ("\n--------------------------------------------------------")
    
asked by anonymous 31.10.2018 / 21:55

1 answer

1

I think you got lost in the logic of your code. You print a message that values less than 0 are invalid, but stores it anyway. Additionally, the error message is only printed if the first value is invalid, and ignores all others.

The quantity could be treated this way

for i in range(10):
    item = int(input("\nQuantidade de itens do %dº representante: " % (i+1)))
    while item < 0:
        item = int(input("\nA quantidade não pode ser menor que zero, digite novamente: "))

And commissions could be stored in an array, as well as the amount of items sold

comissoes = []
for item in itens:
    comissao = '10%' if item < 20 else \
               '15%' if item < 50 else \
               '20%' if item < 75 else \
               '25%'
    comissoes.append(comissao)
    
31.10.2018 / 23:25