Conditional structure does not work

Asked

Viewed 143 times

2

Several times, especially when the second price is higher than the others, the program accuses that it is the cheapest.

#Faça um programa que pergunte o preço de três produtos e informe qual produto 
#você deve comprar, sabendo que a decisão é sempre pelo mais barato.


p1 = input("Digite o 1° preço: ")
p2 = input("Digite o 2° preço: ")
p3 = input("Digite o 3° preço: ")

if p1 < p2 and p1 < p3:
    print(f'O produto mais barato é o {p1}')
elif p2 < p1 and p3:
    print(f'O produto mais barato é o {p2}')
elif p3 < p2 and p3 < p1:
    print(f'O produto mais barato é o {p3}')
else:
    if p1 == p2 and p1 < p3:
        print(f'O produto mais barato é o {p1} e o {p2}')
    elif p1 == p3 and p1 < p2:
        print(f'O produto mais barato é o {p1} e o {p3}')
    elif p2 == p3 and p2 < p1:
        print(f'O produto mais barato é o {p2} e o {p3}')
    elif p1==p2 and p2==p3:
        print('Todos os produtos tem o mesmo valor.')

2 answers

4


The biggest problem is that you are not doing a full condition check on the second if. But the code has problems.

One of them is not converting to integer, so a number like 10 will be less than 2 for example because considering string will be buying initially the first character of each text, ie comparing "1" with "2" and 1 is less than 2, so it is known that that text is smaller and does not need to check the rest. A frequent error is that people confuse textual representation of a number with a fact number. I preferred not to treat the typing error, but if someone enters something other than a number the application will break. This can look like next exercise.

The statement says nothing about treating the tie differently and makes little sense, and even if you make this logic is wrong and gives wrong result. As the logic is only say the cheapest is just take it and will always give the expected result. If you really want to insist on this you need another logic, to even treat the tie of two numbers only.

p1 = int(input("Digite o 1° preço: "))
p2 = int(input("Digite o 2° preço: "))
p3 = int(input("Digite o 3° preço: "))
if p1 < p2 and p1 < p3:
    print(f'O produto mais barato é o {p1}')
elif p2 < p1 and p2 < p3:
    print(f'O produto mais barato é o {p2}')
elif p3 < p2 and p3 < p1:
    print(f'O produto mais barato é o {p3}')

Behold working in the ideone. And in the Coding Ground. Also put on the Github for future reference.

2

Another alternative to this problem, and also much more scalable, is to use the language’s own data structures to solve the problem:

ITEMS = 3
precos = []

for item in range(1, ITEMS + 1):   
    precos.append(int(input(f"Preço do {item}º item = ")))

menor_preco = min(precos)

print(f"O produto mais barato é o {menor_preco}")

In this case what you do is receive all the values in a list and take from them the lowest value with the function min(). The advantage of this approach, besides the aforementioned scalability because it works for 3, 30 or 3000 prices, is that the code is much cleaner and readable.

Browser other questions tagged

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