"Elif" does not allow to execute what is expected

Asked

Viewed 71 times

2

If you do if each one alone and run with else, the calculation is correct. But using both (if and elif) does not recognize the differences and does not display the expected results. There will only be the discount of R $ 7,00 if the price is between the number 23 and the age typed.

idade = int(input('Qual sua idade? '))
preço = float(input('Informe o valor: '))

#Desconto, deve estar entre o número 23 e a idade do cliente.

desconto = float(7)
if preço >= 23:
    print('Você terá um desconto de: R${:.2f}'.format(desconto))
elif preço <= idade:
    print('Você terá um desconto de: R${:.2f}'.format(desconto))
else:
    print('sem desconto')

1 answer

3


Read the description of the criteria that should be used to give the discount. It has a and there, and that’s what you should do. There are no two different conditions, there is only one composed of two parts, it has limits of the track to be considered.

Never use two command blocks to perform exactly the same action, just one. So it makes no sense to do the elif shall create a condition consisting of the two parts of what is described which shall be.

Use a and to concatenate the two parts:

idade = int(input('Qual sua idade? '))
preço = float(input('Informe o valor: '))
desconto = 7.0
if preço >= 23 and preço <= idade:
    print('Você terá um desconto de: R${:.2f}'.format(desconto))
else:
    print('sem desconto')

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

In Python it is possible to do so too:

if 23 <= preço <= idade:

Browser other questions tagged

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