Sum of invoice series (a1 ,an ,n )

Hi, need some help with home work i have.
need to wright a code of a function that sum of invoice series
def SumIN(a1 ,an ,n )
the demands are : if n=0 ,print that the sum is 0
and if n=1, print that the sum is 1
and else, print 𝑛(π‘Ž1+π‘Žπ‘›)/2 .

a1 - first number
an - last numer
n - the number of the series

that what i did so far :

def question1(a1, an, n):
    for i in range(a1,n+1):
        if n == 0:
            print('the sum is 0')
            if n == 1:
                print('the sum is', a1)
        else:
            print("The sum is :" ,n*(a1+an)/2)

It gives me an error onto this line
for i in range(a1, n+1):
The argument may be an int eger or a floating point number.
If u have n+1 in your argument the code will fail

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The β€œpreformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

indentation is really important in python, the second if statement is reached only if the first one is executed (you have put the second if statement inside the first)

I also suggest you take a look at elif statements

1 Like