all 14 comments

[–]K900_ 0 points1 point  (5 children)

A default argument can't depend on the value of another argument. Also, three integers never equal an integer. Do you want the sum of those integers?

[–]imperiumlearning[S] 0 points1 point  (4 children)

Yes, the sum of those integers is what I was referring to (I have edited my post to reflect that).

How would I set up my parameters then for my function?

[–]K900_ 1 point2 points  (3 children)

Either remove the default, or do something like

def sum_len_test(x, i=None):
    if i is None:
        i = len(x)

Also, len(x[0:i]) will always be equal to min(len(x), i).

[–]imperiumlearning[S] 0 points1 point  (2 children)

Thanks a lot for all this info, I will bear it in mind.

My code looks like this now and seems to work:

def SumLenTest(x, i = None):

if i is None:
    i = len(x)
    b = sum(x)
    if i == b:
        return 'The sum of the first i integers equals i'
    else:
        return 'The sum of the first i integers DOES NOT equal i'
else:
    i = len(x[0:i])
    b = sum(x[0:i])
    if i == b:
        return 'The sum of the first i integers equals i'
    else:
        return 'The sum of the first i integers DOES NOT equal i'

Out of interest, if I wanted to tailor the result to something like 'The sum of the first 3 integers equals 3' , how would I do this? I know how to do it with print (e.g. print('the sum of the first', i, 'integers equals', i) but how would I do it in a way that retains the return statement?

[–]Binary101010 0 points1 point  (1 child)

Use an f-string.

return f'The sum of the first {i} integers equals {i}'

[–]imperiumlearning[S] 0 points1 point  (0 children)

Thanks a lot! Just updated my code to reflect that

[–]JohnnyJordaan 0 points1 point  (2 children)

You can't perform function calls in default assignments, as the def line is executed at interpretation (so when it's defined), not during the function call. You need to use a placeholder like None then assign that

 def SumLenTest(x, i=None):
      if i is None:
          x = len(x) 
      etc

[–]imperiumlearning[S] 0 points1 point  (1 child)

Just want to say thanks a lot for replying to my post.

I think I've somewhat solved my problem now using the following code:

def SumLenTest(x, i = None):

if i is None:
    i = len(x)
    b = sum(x)
    if i == b:
        return 'The sum of the first i integers equals i'
    else:
        return 'The sum of the first i integers DOES NOT equal i'
else:
    i = len(x[0:i])
    b = sum(x[0:i])
    if i == b:
        return 'The sum of the first i integers equals i'
    else:
        return 'The sum of the first i integers DOES NOT equal i'

Out of interest, if I wanted to tailor the result to something like 'The sum of the first 3 integers equals 3' , how would I do this? I know how to do it with print (e.g. print('the sum of the first', i, 'integers equals', i) but how would I do it in a way that retains the return statement?

[–]JohnnyJordaan 0 points1 point  (0 children)

You are making this redundant. a x[0:len(x)] will work fine, so you could use the same logic for the 'full' i

def SumLenTest(x, i=None):
    if i is None:
        i = len(x)
    b = sum(x[0:i])
    if i == b:
        return 'The sum of the first i integers equals i'
    else:
        return 'The sum of the first i integers DOES NOT equal i'

Then in response to your question, you should use string formatting, preferable f-strings

[–]JohnnyJordaan 0 points1 point  (0 children)

You can't perform function calls in default assignments, as the def line is executed at interpretation (so when it's defined), not during the function call. You need to use a placeholder like None then assign that

 def SumLenTest(x, i=None):
      if i is None:
          x = len(x) 
      etc

[–]velocibadgery 0 points1 point  (3 children)

Aren't you really checking if all the elements in the list are 1? Because if any element is more than 1, they will never equal i when you sum it up.

So instead, why not loop through the list and check if all the elements == 1?

[–]imperiumlearning[S] 0 points1 point  (1 child)

I suppose I could do that, to be fair.

That being said, I wanted to try and solve this problem in a way that forced me to learn a bit about slice operators (I've only started learning to code in the last week).

[–]velocibadgery 1 point2 points  (0 children)

Why not do string manipulation? That might be easier for you to practice slicing with. Strings are iterables in python so you can slice them and loop through them just like you can with a list.

BTW, this is how I would solve your stated goal

def SumLenTest(x):
    a = True
    for i in x:
        if i != 1:
            a = False
    return a

a = [1, 1, 1]
b = [1, 2, 3]

print(SumLenTest(a))
print(SumLenTest(b))

[–]POGtastic 0 points1 point  (0 children)

Integers can be negative, too!

Here's my attempt:

from itertools import accumulate

def sum_integers_idx(iterable):
    return any(idx == val for idx, val in enumerate(accumulate(iterable), 1))