Reputation: 13
Edit: I should add that the focus for this problem is on the use of 'side effects' in a function.
I am submitting my code on my computing course on Coursera. I think I must be missing something super obvious. When I submit my code I get the following error:
#TEST 5#
append_fibonacci(args) returned None
** ERROR ** side effect from: [-5, 8, 1] to: [-5, 8, 1]
* EXPECTED: * [-5, 8, 3]
inputs:
outputs:
I mostly just don't understand what the feedback is telling me. I'm a bit of a newbie, having just started Python this month. Thank you for your help!
The following is my code:
def append_fibonacci(integer_list):
# Modify the argument list of integers by
# appending a new integer that is the sum
# of the last two integers in the list.
# If the list has fewer than two elements
# add the int object 1 to the list.
if len(integer_list) > 2:
sum_last_two = ((integer_list[-1]) + (integer_list[-2]))
integer_list.append(sum_last_two)
else:
integer_list.append(1)
def main():
# Call the append_fibonacci function on this
# list: [3, 5, 8] and output the result object
number_list = [3, 5, 8]
append_fibonacci(number_list)
print(number_list)
main()
Upvotes: 0
Views: 595
Reputation: 780724
The error message you got is saying that the updated list should be [-5, 8, 3]
but you're setting it to [-5, 8, 1]
This is because your function doesn't work correctly if the list has exactly 2 elements. It should add the two elements and append the sum (-5 + 8 = 3
), but it will instead just append 1
.
if len(integer_list) > 2:
should be:
if len(integer_list) >= 2:
Upvotes: 1