Python Forum
what am i doing wrong? i have SyntaxError: invalid syntax
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what am i doing wrong? i have SyntaxError: invalid syntax
#1
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
Reply
#2
You need a space between "if" and the underscore.
Reply
#3
THANK YOU MICSEYSDEL IT WORKED
Reply
#4
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print(data) is suddenly invalid syntax db042190 6 1,182 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  Python Pandas Syntax problem? Wrong Output, any ideas? Gbuoy 2 920 Jan-18-2023, 10:02 PM
Last Post: snippsat
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,533 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  SyntaxError: invalid syntax ?? korenron 15 5,704 Jan-25-2022, 11:46 AM
Last Post: korenron
  Invalid syntax with an f-string Mark17 7 7,769 Jan-14-2022, 04:44 PM
Last Post: Mark17
  invalid syntax in my class CompleteNewb 2 1,889 Dec-13-2021, 09:39 AM
Last Post: Larz60+
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 3,161 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Unexplained Invalid syntax Error cybertooth 5 3,242 Aug-02-2021, 10:05 AM
Last Post: cybertooth
  [split] SyntaxError: invalid syntax Code_X 3 2,750 May-04-2021, 05:15 PM
Last Post: Yoriz
  Invalid syntax error - need help fixing calgk01 3 3,270 Feb-23-2021, 08:41 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020