1
I can’t do this exercise, not with while or with gone. If you can make it available to me the way it’s done with While and For,.
- Given a list of numbers, tell us the highest number on the list. Use the Len( ) function to find out the list size and the while repeat structure to scroll through the list.
Remembering that the program should be done exactly as requested in the exercise.
Code I managed to do, but super wrong:
lista = [0, 10, 20, 50, 80]
maior = lista[0]
while maior in lista < maior:
print(maior)
It worked, but why did the if look like this? And in the same row of the biggest = list[i]?
– Pigot
This is a ternary. It works like this, if the condition is met (
maior < lista[i]
), the variablemaior
receives the amountlista[i]
otherwise, she gets the valuemaior
(herself, in case).– Jéf Bueno
I changed it to be clearer, @Pigot
– Jéf Bueno
Is there any way to do it other than by ternary? I edited my code above, it just prints the last, not the biggest ( what I edited)
– Pigot
I just switched to a normal if
– Jéf Bueno
What a code, @Pigot?
– Jéf Bueno
It worked, thank you very much! :)
– Pigot
A detail: it is good that you start
maior
with the first item on the list, because if the list has only negative numbers, zero will still be the largest - and there may not even be a zero on the list.– Woss
Thank you, @Andersoncarloswoss
– Jéf Bueno
Makes sense, haha, thank you!
– Pigot