if and Else in Python

Asked

Viewed 124 times

1

Hello, I have a question about the command order. My script went like this:

if salario <= 1250:
    aumento = (salario * 15 / 100) + salario
    print('Seu salário com reajuste é R${:.2f}'.format(aumento))
else:
    aumento = (salario * 10 / 100) + salario
    print('Seu salário com reajuste é R${:.2f}'.format(aumento))

If I change the order of the calculation to

if salario <= 1250:
    aumento = (salario * 10 / 100) + salario
    print('Seu salário com reajuste é R${:.2f}'.format(aumento))
else:
    aumento = (salario * 15 / 100) + salario
    print('Seu salário com reajuste é R${:.2f}'.format(aumento))

It returns me the wrong value. Can anyone tell me if there is any statement order

3 answers

2

The if/else is a conditional selection structure used to divert processing flow between command blocks.

Your algorithm is:

Se (condição) Então
    (bloco de código Se)
Senão
    (bloco de código Senão)
Fim Se

Where condição is a logical expression whose result may be either Verdadeiro or Falso, and that outcome will decide which of the blocos de código will be executed. Follows the visual representation of the conditional selection structure if/else in a flow diagram:

Fluxograma if/else

By applying this knowledge in your code we can comment on it to facilitate its interpretation for human readers:

#Verifica se salario é menor ou igual a 1250
if salario <= 1250:
    #Se for menor ou igual a 1250... 
    aumento = (salario * 15 / 100) + salario #reajusta em 15%
    print('Seu salário com reajuste é R${:.2f}'.format(aumento)) #Imprime salário reajustado
else:
    #Se for maior que 1250... 
    aumento = (salario * 10 / 100) + salario #reajusta em 10%
    print('Seu salário com reajuste é R${:.2f}'.format(aumento)) #Imprime salário reajustado

By understanding how each part of the code works it is possible to check for flaws or redundancies in the code. We can see that the line where you print the salary is exactly the same in both blocks:

print('Seu salário com reajuste é R${:.2f}'.format(aumento)) #Imprime salário

In this case we can improve the code by applying a programming principle called DRY or Do not repeat yourself whose philosophy is to remove code redundancies

#Verifica se salario é menor ou igual a 1250
if salario <= 1250:
    #Se for menor ou igual a 1250... 
    aumento = (salario * 15 / 100) + salario #reajusta em 15%
else:
    #Se for maior que 1250... 
    aumento = (salario * 10 / 100) + salario #reajusta em 10%

#Imprime salário com reajuste independentemente de qual seja
print('Seu salário com reajuste é R${:.2f}'.format(aumento))

1

In the if structure only one of the blocks is executed, in the case if the first condition is true(if) then the if block will be executed. The Else block will only be executed if the first condition is false. In the example, to have the same result as the first one you must change the condition to if(salary > 1250). Thus the increase will be 15% for wages of up to 1250 and 10% for higher wages. Otherwise in the two examples if the salary is exactly 1250 the first block will be executed (if).

0


if salario <= 1250:
    aumento = (salario * 15 / 100) + salario #reajusta em 15%
else:
    aumento = (salario * 10 / 100) + salario #reajusta em 10%

print('Seu salário com reajuste é R${:.2f}'.format(aumento))

In your first if you say: if the salary is less than or equal to 1250, give a 15% increase, if it is higher, increase to 10%. In your second if you say: if the salary is less than or equal to 1250, give a 10% increase, if it is higher, increase to 15%. If you changed the calculations, you must change the condition, too. The second would be:

if salario > 1250: 
    aumento = (salario * 10 / 100) + salario 
else: 
    aumento = (salario * 15 / 100) + salario 
print('Seu salário com reajuste é R${:.2f}'.format(aumento))

Browser other questions tagged

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