Here is a snippet, in the code through the top- down way, I printed the type of the variable named "podium") this is the code:
def frequence(entranche):
podium = []
print("premier podium", type(podium))
for item in entranche:
print ("deuxieme podium", type(podium))
scat = len(entranche)
for indice in range (len(entranche)):
if entranche[indice] == item:
scat -= 1
frequence = len(entranche) - scat
podium = podium.append(frequence)
print("troisieme podium", type(podium))
plus_haute_frequence = max(podium)
return(plus_haute_frequence)
print(frequence("Je suis né dans le béton Coincé entre deux maisons".split()))
after the code, this is the output:
premier podium <class 'list'>
deuxieme podium <class 'list'>
troisieme podium <class 'NoneType'>
I don't understand why the podium variable loses his type.
Someone said me:
"Now, i think your problem is this: troisieme podium - right? It's because of podium = podium.append(frequence). Just append the new value to your list, re-assigning the variable istn't correct here. Just do podium.append(frequence)".
It's right. but I don't understand why. possibly we consider this as a new variable. And what can I do if I need to make e.g. something with a method that do not alter the named variable until writing variable = variable.method() {in ruby language there is method with "!" and without '!"}
This is because the append()
method modifies the list in place. It doesn't return a new list, but modifies the list given as parameter. Its return value is None, and that is why your 'troisieme podium' type is NoneType.
>>> a = [1, 2, 3]
>>> a.append(4)
>>> a
[1, 2, 3, 4]
>>> print(a.append(5))
None
>>> a
[1, 2, 3, 4, 5]
So this line:
podium = podium.append(frequence)
should be simply:
podium.append(frequence)