Conditional structure does not work

3

Several times, especially when the second price is higher than the others, the program charges 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.')
    
asked by anonymous 31.12.2018 / 20:05

2 answers

5

The biggest problem is that you are not doing a complete 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 initially buying the first character of each text, ie comparing "1" with "2" and 1 is less than 2, so we already know that the text is smaller and does not need to check the rest. A common mistake is for people to confuse textual representation of a number with a fact number. I preferred not to handle the typing error, but if someone types something other than a number the application will break. This may be the next exercise.

The statement does not say anything about treating the tie differently and makes little sense, and even if you do this logic is wrong and gives the wrong result. As the logic is just to say the cheapest is just take it off and always give the expected result. If you really want to insist on it you need another logic, to even deal with 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}')

See running on ideone . And no Coding Ground . Also I put GitHub for future reference .

    
31.12.2018 / 20:27
0

Another alternative to this problem, and also much more scalable, is to use the data structures of the language itself 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 to receive all the values in a list and get the lowest value with the min() function. The advantage of this approach, in addition to the already mentioned scalability since it works for 3, 30 or 3000 prices, is that the code is much cleaner and readable.

    
01.01.2019 / 13:02