Python 为什么代码在尝试创建类的对象时显示SyntaxError:无效语法?

Python 为什么代码在尝试创建类的对象时显示SyntaxError:无效语法?,python,Python,我创建了一个名为Boy的类,并尝试创建一个对象b1(b1=Boy()),但此行显示了错误 完整代码如下: class Boy: name='JOHN' age='21' def say_hello(self): print('Hello! My name is %s and I am %s years old' % (self.name,self.age) b1=Boy() b1.say_hello() runfile('C:/Users/kesha/.

我创建了一个名为Boy的类,并尝试创建一个对象b1(b1=Boy()),但此行显示了错误

完整代码如下:

class Boy:
    name='JOHN'
    age='21'
    def say_hello(self):
        print('Hello! My name is %s and I am %s years old' % (self.name,self.age)
b1=Boy()
b1.say_hello()
runfile('C:/Users/kesha/.spyder-py3/edugrad.py', 
wdir='C:/Users/kesha/.spyder-py3')
Traceback (most recent call last):

File "C:\ProgramData\Anaconda3\lib\site- 
packages\IPython\core\interactiveshell.py", line 3325, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)

File "<ipython-input-1-d5ee685f3657>", line 1, in <module>
runfile('C:/Users/kesha/.spyder-py3/edugrad.py', 
wdir='C:/Users/kesha/.spyder-py3')

File "C:\ProgramData\Anaconda3\lib\site- 
packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site- 
packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/kesha/.spyder-py3/edugrad.py", line 13
b1=Boy()
 ^
SyntaxError: invalid syntax
错误消息如下:

class Boy:
    name='JOHN'
    age='21'
    def say_hello(self):
        print('Hello! My name is %s and I am %s years old' % (self.name,self.age)
b1=Boy()
b1.say_hello()
runfile('C:/Users/kesha/.spyder-py3/edugrad.py', 
wdir='C:/Users/kesha/.spyder-py3')
Traceback (most recent call last):

File "C:\ProgramData\Anaconda3\lib\site- 
packages\IPython\core\interactiveshell.py", line 3325, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)

File "<ipython-input-1-d5ee685f3657>", line 1, in <module>
runfile('C:/Users/kesha/.spyder-py3/edugrad.py', 
wdir='C:/Users/kesha/.spyder-py3')

File "C:\ProgramData\Anaconda3\lib\site- 
packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site- 
packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/kesha/.spyder-py3/edugrad.py", line 13
b1=Boy()
 ^
SyntaxError: invalid syntax
runfile('C:/Users/kesha/.spyder-py3/edugrad.py',
wdir='C:/Users/kesha/.spyder-py3')
回溯(最近一次呼叫最后一次):
文件“C:\ProgramData\Anaconda3\lib\site-
packages\IPython\core\interactiveshell.py”,第3325行,运行代码
exec(代码对象、self.user\u全局、self.user\n)
文件“”,第1行,在
runfile('C:/Users/kesha/.spyder-py3/edugrad.py',
wdir='C:/Users/kesha/.spyder-py3')
文件“C:\ProgramData\Anaconda3\lib\site-
packages\spyder\u kernels\customize\spyderrcustomize.py”,第827行,在运行文件中
execfile(文件名、命名空间)
文件“C:\ProgramData\Anaconda3\lib\site-
packages\spyder\u kernels\customize\spyderrcustomize.py”,第110行,在execfile中
exec(编译(f.read(),文件名,'exec'),命名空间)
文件“C:/Users/kesha/.spyder-py3/edugrad.py”,第13行
b1=男孩()
^
SyntaxError:无效语法
我尝试使用Jupyter笔记本在python控制台中运行相同的代码,结果运行得很好。


预期输出:您好!我叫约翰,今年21岁。

只是几个小虫子。首先,您忘记了print语句的最后一个括号。这实际上是造成你的错误的原因。有时,像这样的错误很难跟踪。很常见的情况是忘记一个结束括号/方括号/大括号,这会导致下一行报告错误(认为这是前一行的一部分)

第二,为了让类按照您想要的方式工作,使用初始化方法将变量与self关联更符合python(而且更合适)

最后,随着Python 2.7将于2020年面世,建议使用Python的
format
命令格式化字符串

class Boy:
    def __init__(self):
        self.name='JOHN'
        self.age='21'

    def say_hello(self):
        #print('Hello! My name is %s and I am %s years old' % (self.name,self.age))
        print('Hello! My name is {} and I am {} years old'.format(self.name,self.age))
b1 = Boy()
b1.say_hello()

数一数括号前面的行中缺少一个
。投票结束是一个打字错误。如果语法错误没有任何意义,请检查它前面的行。