def append_sum(lst):
for x in range(3):
lst.append(lst[-1] + lst[-2])
print(lst)append_sum([1, 1, 2])
The above code gives:
[1, 1, 2, 3, 5, 8]
none
why “none”? how will be the correct code using a loop?
def append_sum(lst):
for x in range(3):
lst.append(lst[-1] + lst[-2])
print(lst)append_sum([1, 1, 2])
The above code gives:
[1, 1, 2, 3, 5, 8]
none
why “none”? how will be the correct code using a loop?
Try return
ing the list, then print it at the caller.
print (append_sum([1, 1, 2]))
A Python function will always have a return value. There is no notion of procedure or routine in Python. So, if you don’t explicitly use a return value in a return
statement, or if you totally omit the return
statement, then Python will implicitly return a default value for you. That default return value will always be None
.
as mtf said put in a return statement.
read more on return here
really appreciate the recommendation, thanks!