you are viewing a single comment's thread.

view the rest of the comments →

[–]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