2
I want to display the sum of the numbers typed as well as the largest and smallest number typed, only with while
, if
and else
:
contador = 1
maior = 0
menor = 0
S = 0 # 1º NÚMERO A SER SOMADO
while contador <= 5:
num = int(input("Digite o " + str(contador) + "º valor:"))
if num >= maior:
maior = num
if maior < num:
menor = num
S += num
contador += 1
print("A soma de todos os valore é igual a", S)
print("O maior valor digitado foi", maior)
print("O menor valor digitado foi", menor)
The program shows the sum and the highest value, but does not show the lowest value.
Did you debug the code? Did you test with chosen numbers and track what each line of your code does? I would say that the main problem of your code can be divided into two parts: 1) you test
maior
withnum
to definemenor
(should testmenor
); 2) the value ofmenor
already starts very low (equal to 0). Unless your input contains negative numbers, it will never have a lower value than this. Programming requires "experiment" with logic. If you don’t do this, you will hardly learn correctly.– Luiz Vieira
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).
– Maniero