Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
invalid syntax error
#1
Hi Everyone

I'm taking a basic coding class, and while I like it so far, my teacher doesn't really have a knack for teaching, so I'm finding it very difficult to understand. We're translating mathematical expressions into Python and I'm only on #2 and struggling. Lol.

I'm a little embarrassed to show what I've tempted, but whatever.

So this is the expression: n(n-1)/2
And here is the terrible code I attempted:
def main():
n=float(input("Enter value for n: "))
ans1=n*(n-1)
ans2=ans1/2
print("The answer is:", ans2)
And then I get an error when I try to run it. I figure the trouble is the middle part when I try and solve the expression, but Idk how to fix it.
Reply
#2
you forgot to indent:
def main():
    n = float(input("Enter value for n: "))
    ans1 = n*(n-1)
    ans2 = ans1/2
    print("The answer is:", ans2)
main()

Enter value for n: 25
The answer is: 300.0
Reply
#3
I actually did indent in Python, I just forgot to do it on here. It still tells me invalid stynax. Appart form the indent, the rest looks good though?
Reply
#4
Copy and try my code,
also, post actual error.
Python version?
Reply
#5
If the formula is n * (n - 1) / 2 , why did you break it into two parts rather than keep it as is?

def main():
    n = float(input("Enter value for n: "))
    ans = n * (n - 1) / 2
    print("The answer is:", ans)

main()
Output:
Enter value for n: 25 The answer is: 300.0
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked School project -- need help with it it says syntax error XPGKNIGHT 6 3,322 Aug-11-2022, 08:43 PM
Last Post: deanhystad
  Invalid syntax Slome 2 1,970 May-13-2022, 08:31 PM
Last Post: Slome
  print(f"{person}:") SyntaxError: invalid syntax when running it AryaIC 11 16,369 Nov-07-2020, 10:17 AM
Last Post: snippsat
  I'm getting a syntax error very early on in my code and I can't quite figure it out. liloliveoil 1 2,005 Oct-30-2020, 05:03 AM
Last Post: deanhystad
  Unspecified syntax error hhydration 1 2,008 Oct-25-2020, 10:45 AM
Last Post: ibreeden
  Annuity function for school - syntax error peterp 2 1,979 Oct-12-2020, 10:34 PM
Last Post: jefsummers
  Invalid syntax error, where? tucktuck9 2 3,426 May-03-2020, 09:40 AM
Last Post: pyzyx3qwerty
  Raise an exception for syntax error sbabu 8 3,161 Feb-10-2020, 01:57 AM
Last Post: sbabu
  syntax error: can't assign to operator liam 3 4,052 Jan-25-2020, 03:40 PM
Last Post: jefsummers
  Self taught , (creating a quiz) syntax error on my array DarkAlchemyXEX 9 4,269 Jan-10-2020, 02:30 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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