+ 1
Appending in Python. Not working
cars = ['audi', 'vw', 'subaru'] cars.append('fiat') len(cars) . #nothing happens here Nothing happens. (No counting) Why??
7 Réponses
+ 3
why would you expect something to happen? you don't print out the length at all
+ 2
Max Rubs because there, code you type is forwarded into the output immediately, so it just prints it no matter what, and as append returns void, nothing is printed previously (iirc)
+ 1
but if you add:
print(len(cars))
it should output 4
the code itself is fine. but as hinanawi said, len(object) does not produce anything.
+ 1
TY Marco! It works!
Don't you know why my way works in IDLE?
0
I meant it doesn't count anything((
0
Max Rubs len() returns an integer, so you need to store the value somewhere for anything to happen
0
Strange, it works in the python shell
>>> cars = ['audi', 'vw', 'subaru']
>>> cars.append('fiat')
>>> len(cars)
4