Python Script Creation 3

Asked

Viewed 137 times

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 order value; - for applications of 20 and 49 items, the commission is 15% of the total value of the application; - for applications of 50 to 74 items, the fee is 20% of the total value of the application; and - for applications of 75 items or more the commission shall be 25%.

Make a program that reads the number of order items of each representative and prints the commission percentage of each.

I thought of creating a variable for each entry, ie a variable for each representative and at the end to generate the commissions by placing several if Elif and Else. My question is how to get this code thinner without me having to create the 10 variables?

I sort of made this piece of code that gives me the 10 entries. But from now on, I’ve tangled up to create the conditionals to generate the value of each commission from each representative.

My code sketch, 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--------------------------------------------------------")

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 you store it anyway. Also 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)

Browser other questions tagged

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