Python Code - Conditional Structure

Asked

Viewed 361 times

3

As I do in Python to receive the value of the base salary and the sales value of this seller, the employee’s basic salary should be up to R $ 700.00 with a 4.5% bonus on their sales and if the sales volume is greater than R $ 15.000,00 wins a 1.2% sales bonus.

For now my code is like this:

sal = float ("Insira salário: "()) 

ven = float ("Insira valor de vendas: "()) 

if sal <= 700 and ven >= 15000.00:

bonisal = 0.045

boniven = 0.012

calsal = (salario * bonisal) * 100

calven = (vendas * boniven) * 100
  • I made more lines, update the page. Now I wonder if it’s all in the same condition.

4 answers

3

First, the "function" (which is a class) float does not read the program input. That is, you cannot ask the user a value through it, for this you will need the function input (raw_input in Python 2). Use float only to define a numerical value from the read value. We can add to the process a validation to ensure that the reported salary is less than or equal to 700, as stated in the statement:

while True:
    try:
        salario = float(input('Informe o salário: R$ '))
        if not 0 < salario <= 700.00:
            raise ValueError('Salario precisa positivo e até 700')
        break
    except ValueError as error:
        print(error)

Thus the user will be asked until he enters a valid value:

>>> Informe o salário: R$ a
could not convert string to float: 'a'
>>> Informe o salário: R$ 800
Salario precisa positivo e até 700
>>> Informe o salário: R$ 500

We do the same for the value sold, but now without the validation of the range:

while True:
    try:
        vendas = float(input('Informe o valor em vendas: R$ '))
        break
    except ValueError as error:
        print(error)

Statement says there is a 4.5% bonus on the sale value:

bonificacao = 4.5 / 100

Plus a 1.2% increase if sales exceed 15,000:

if vendas > 15_000:
    bonificacao += 1.2 / 100

Therefore the final amount of the salary will be:

final = salario + vendas * bonificacao

1


If you want a simple code without having so many checks you can simply use this code:

sal = float(input('Insira o salário: '))
vendas = float(input('Insira o valor de vendas: '))
while True:
    if sal < 700:
        break
    else:
        print('Salario precisa ser positivo e até R$700')
        sal = float(input('Salário: '))

bonificacao = vendas * (4.5/100)
if ven > 150000:
    bonificacao += vendas * (1.2/100)

calcvendas = sal + bonificacao
print(calcvendas)
  • Thank you very much, I really understood this code better but I realized that it is wrong in the line that has "if ven > 15000" instead of "if sales > 150000" but my teacher is still in conditional structure and you used the repeating structure while

  • @Sonogoku, got it. I’ll edit the question to answer your question :)

1

TL;DR

My suggestion consists of a function that can receive the percentages for the bonuses as optional parameters and returns a dictionary with the values informed by the user and the calculations performed.

def calcvendas(bnf=4.5, bnf_adicional=1.2):
    salario, vendas = -1, -1
    while salario<0 or salario>700:
        try:
            salario = float(input('Digite o salário (até 700): '))
        except ValueError as error:
          print(error) 
    while vendas<0:
        try:
            vendas = float(input('Insira o valor de vendas (>=0): '))
        except ValueError as error:
            print(error) 
    bonificacao = vendas * (bnf/100) 
    adicional = 0 if vendas <= 15000 else vendas * (bnf_adicional/100)
    return {'Salario': salario, 'Vendas': vendas, 
            'Bonificacao': bonificacao, 'Adicional': adicional, 
            'Total': salario+bonificacao+adicional}

print(calcvendas())    

In case the user has typed 500 for salary and 16000 for sales, the output would be:

{'Salario': 500.0,
 'Vendas': 16000.0,
 'Bonificacao': 720.0,
 'Adicional': 192.0,
 'Total': 1412.0}

Note that in my understanding, in case the seller reaches the goal of exceeding 15k of sales, he earns the additional on the sale value.

See working on repl it.

0

Brother, I am without Python here to check if the code is running without bugs, but, if I didn’t understand wrong what you asked, this code here will work:
(checks if there is no syntax error because I wrote in the block)

sal = float (input('Insira o salário do vendedor (de no máximo R$ 700,00): '))  
ven = float (input('Insira o valor de vendas realizadas: '))  
if ven <= 15000:  
    sal_com_bonificação = sal +((ven/100) * 4.5)  
elif ven > 15000:  
    sal_com_bonificação = sal +((ven/100) * 1.2)  
print('O salário do vendedor que ganha {} e vendeu {} será {}'.format(sal; ven; sal_com_bonificação))  

PS: Remember to indent the conditions below IF and ELIF.

Browser other questions tagged

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