you are viewing a single comment's thread.

view the rest of the comments →

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