Asked
Active
Viewed 28 times
-2
Tasawar Saraf
- 3
- 2
-
3Can you make this a [mcve] by posting the code and the intended output as text? – SuperStormer Apr 02 '20 at 23:44
-
Please repeat the intro tour, especially [how to ask](https://stackoverflow.com/help/how-to-ask). It is not acceptable to give us only a link to an off-site question. – Prune Apr 02 '20 at 23:47
-
Isn't this a Fibonacci problem?https://stackoverflow.com/questions/33325683/python-creating-a-list-of-the-first-n-fibonacci-numbers – mad_ Apr 02 '20 at 23:52
1 Answers
0
The problem is just another option for computing three elements of the Fibonacci sequence.
Alternate code to yours
def append_sum(lst):
for i in range(3): # Loop 3 times
lst += [sum(lst[-2:])] # add sum of last two elements to list
# lst[-2:] is last two elements of list
# sum(...) is the sum
# x += y appends y to x when x and y are lists
return lst
print(append_sum([1, 1, 2]))
Output
[1, 1, 2, 3, 5, 8]
DarrylG
- 14,084
- 2
- 15
- 21
-
Glad I could help. [For loops](https://www.w3schools.com/python/python_for_loops.asp) are easy. You may want to add your code to the question rather than the image if possible. – DarrylG Apr 03 '20 at 00:57