Calling function multiple time

As you can see i’m struggling to write elegant code here. Of course, i can easily “summary = sum(lst[-2:])
lst = lst + [summary]” write this code 3 times in a row in order to get result here image but i’m trying to write more optimized code here. In C# i can write this code with for loop but here i couldn’t managed to write this code as in c#. I’m keep getting IndentationError. Can anyone help me to write more elegant code here ?

Hello! Could you provide a link to the exercise?

1 Like

What particular exercise is it?

1 Like

Append Sum. The first one

Looks like, “Append Sum”.

This is a precursor to Fibonacci sequence.


Well, you’ve almost got it. You’ve used a loop well-although there doesn’t seem to be much need for the summary_func function. You can just use a loop range(3); the exercise does not ask you to allow for different number of iterations.

1 Like

I know but trying to push my knowledge thank you

Ok, however the use of the second function increases the complexity of the code. If you take that function out, the loop should work on its own. (Possibly with a few adjustments).

1 Like

image I can’t get it why am i still getting IndentationError:

Look at where your return is placed within the function. Is that correctly indented?


No. It should be inline with the for loop in order to be correctly indented.

1 Like

But if i intend it inside of the for loop the list will not return 3 times?

Not inside the loop. Inline with the loop:

def func_xyz(x, y, xyz):
  for x in y:
  return xyz

You see how the for and the return are on the same indentation level?


I get it know. It was my mistake.

Code is working right now. Thank you sooo much.

1 Like

Part of elegance is simplicity…

def append_sum(lst, count=3):
  for _ in '_' * count:
  return lst
1 Like

Much better. Well i’m quite new in coding actually. Thank you for sharing this.

1 Like

The new learners very typically assume more complexity than needed. Work with the least amount of information and code and debugging will be so much easier.

Happy coding!

print(append_sum([1, 1, 2], 6))
[1, 1, 2, 3, 5, 8, 13, 21, 34]

And can i ask a question out of topic ? In C# i was using for loop with parameters like “i” or “j” and i can define a limit. For example for(i=0, i<10, i++) so with this code i can run code 10 times. However, i couldn’t manage to learn the logic behind the “for” loop in python. Why am i using " _ " and range(x) to run certain amount ? For example what does " _ " mean ?

We use the underscore as a variable placeholder. It does not get referenced in the loop. Likewise above I used the __mul__() method (of the str class) to create a string object, count characters long. The loop iterates over that string the same way it would iterate over range(count).

'_' * count    =>  invokes the `str.__mul__()` method
>>> dir(str)
[..., '__mul__', ...]
1 Like

The range(X) acts somewhere akin to what the loop is C# does. Therefore, the loop loops through the code, for each value of i (or whichever variable). It’s just that the increasing of the numbers is done by range, not i++ (or whichever). The value you pass into range is like the condition i<10.

1 Like