Python Forum

Full Version: what am i doing wrong? i have SyntaxError: invalid syntax
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
def summation(n):
        total = 0
        for count in range(1,n + 1):
                total += count
        return total

def main():
        n = int(input('Enter the number of terms : '))
        total = summation(n)
        print('Sum of first', n, 'positive integers:  ' ,total)
if__name__=='__main__':
        main()

THE ERROR MESSAGE IS
Error:
Error(s), warning(s): File "source_file.py", line 11 if__name__=='__main__': ^ SyntaxError: invalid syntax
You need a space between "if" and the underscore.
THANK YOU MICSEYSDEL IT WORKED
Indentation should be 4-space not 8 and i trow in f-string.
def summation(n):
    return sum(range(1, n+1))

def main():
    n = int(input('Enter the number of terms : '))
    total = summation(n)
    print(f'Sum of first {n} positive integers: {total}')

if __name__=='__main__':
        main()