简体   繁体   中英

“SyntaxError: Invalid Syntax” but it worked before! (Python 3.5.2 doing a tutorial for beginners)

Soo I swear this code worked before!

Is there a setting on IDLE that I might have accidently turned on?

I'm trying to do something very simple..set variables.

As you can see these work:

>>> print("hello")
hello



>>> def main():
        print("hey")


>>>main()
hey

So when I try to recreate an example problem, you would expect these variables to work, right?

def main():
    print("This calculates the cost of coffee.")
    print()
    n = eval(input("Enter amount of coffee in pounds: ")
    m = 10.50 * n

SyntaxError: invalid syntax

Why??? Why does Python 3.5.2 return "SyntaxError: invalid syntax"?

Thanks guys! Sorry I'm such a noob.

As pointed out in the comments several times, You are simply missing a closing ) on line 4 of your program. line 4 should look like

n = eval(input("Enter amount of coffee in pounds: "))# <--extra parenthesis


Some unrelated points in your code:

  • Why are you using eval() ? It looks like your going for float/integer conversion, so use int() or float() .
  • Instead of adding a extra print to achieve a newline, simply say print("This calculates the cost of coffee.\\n")
  • The last two lines of your program can be condensed into a single statement: n = float(input("Enter amount of coffee in pounds: "))*10.50

After adding my suggestions to your code, it would yield something like this:

def main():
    print("This calculates the cost of coffee.\n")
    n = float(input("Enter amount of coffee in pounds: "))*10.50

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM