This here:
if None in classe1 or classe2 or classe3:
Not does what you think you do. In fact, what you have are 3 conditions:
None in classe1
: this checks out if None
is on the list classe1
(that is, if None
was previously inserted into the list). But since you only insert numbers, the None
will never be on the list, so that condition will always be false
classe2
: yes, a list (and any other value) can be used in a context boolean. In this case, if the list is empty, it is considered a false value, and if it is not empty, it is considered true.
classe3
: ditto
Therefore, the first condition will always be false, but if classe2
or classe3
have at least one element, will be considered True
and will enter the if
, since an expression that has several conditions with or
is considered true if any of the conditions is true (print the lists after this if
and see for yourself classe2
or classe3
is not empty, a zero is always added).
And that’s why he doesn’t get into if
when the lists classe2
and classe3
are empty: because an empty list is evaluated as False
, and as already said, the first condition is also false because None
was not added in classe1
(then all conditions are false and does not enter the if
).
But honestly, what you thought you’d do is - in my opinion - a great gambit. If the idea is to return zero if the list is empty, there is no reason to add an element artificially just to avoid error. I find it simpler to check if the list is empty:
n = int(input())
classe1 = []
classe2 = []
classe3 = []
for _ in range(n):
velocidade = int(input())
if velocidade < 10:
classe1.append(velocidade)
elif velocidade < 20:
classe2.append(velocidade)
else:
classe3.append(velocidade)
def maior(lista):
if len(lista) == 0: # se a lista é vazia, retorna zero
return 0
return max(lista) # lista não é vazia, retorna o max()
print(maior(classe1), maior(classe2), maior(classe3))
I created a function to make it easy: it checks if the list is empty (if it is, returns zero), and if it is not, it uses max
. That’s better than adding one more element. Of course we can also discuss whether it makes sense to return any artifical value (And if only zeros are typed, how will you know if zero is the highest value that was actually typed or if it fell in the case of empty list? Maybe it is the case of not returning anything and printing an error message, for example), but anyway, insert an artificial value "just for the max
make no mistake" seems like a worse option.
I removed the list listaVelocidade
, because it seems that it was not being used for anything (only to store the speeds, to only be inserted in one of the other lists, so it seems to me a little redundant; I found it simpler to insert the direct value in the respective lists).
And I created the lists only once, before the for
. You were creating them inside the for
, that is, at each iteration they were recreated again, which makes no sense. Create once outside the loop and inside it just insert the elements.
Note also the if
's: if you didn’t enter the if velocidade < 10
is because the value is definitely greater than or equal to 10, so you don’t need to test this again in the elif
.
And if you didn’t get into elif velocidade < 20
, is because it is definitely greater than or equal to 20, so do not need to test again in the else
:
if velocidade < 10:
# se entrou aqui, é porque é menor que 10
classe1.append(velocidade)
# se não entrou no if acima, é porque com certeza é >= 10, então não precisa testar de novo no elif
elif velocidade < 20:
classe2.append(velocidade)
# se não entrou no if nem no elif, é porque com certeza é >= 20, então não precisa testar de novo
else:
classe3.append(velocidade)