Search code examples
pythonpython-2.7syntax-errorpi

Python's telling me there's a syntax error, please explain


Invalid Syntax: What has gone wrong?

Basically, I tried to make a Pi calculator using a variety of different methods I've found myself. This is my most recent idea (probably already exists but checking if it does ruins the fun imo).

def pi_calculator(x):
    n = 1.0
    y = 0.0
    while n < x:
        y += 4 * ((-1)**(n - 1) / ((2 * n) + 1)
        n += 1
    return y

print pi_calculator(1000)

Specifically, it's telling me there's a syntax error with n += 1

    n += 1
    ^
SyntaxError: invalid syntax

I'm quite new to Python and have no idea what's going on here, it's saying the same thing on multiple shells or IDEs (whatever they're called); so I'd appreciate any feedback. In advance, I'd like to also say that I have no idea if this program would work - as it stands - so don't pick me up on that please, I'll like to figure it out myself :P

(Using Spyder; Python 2.7)


Solution

  • It has to be

    y += 4 * ((-1)**(n - 1) / ((2 * n) + 1))
    

    The last ) is missed in line 5: which is

    y += 4 * ((-1)**(n - 1) / ((2 * n) + 1)