2 sky54bc sky54bc 于 2014.09.19 10:54 提问

python初学者编程过程中遇到问题

用python编写了个Person类:
''
Created on 2014年9月19日

@author: Administrator
'''

class Person:

def __init__(self,name,age,salary=0,job=None):
        self.name=name
        self.age=age
        self.salary=salary
        self.job=job

def lastName(self):
    return self.name.split()[-1]

def giveRaise(self,percent):
    self.salary *= (1.0+percent)

if __name__ == '__main__' :
        bob = Person('tom',22,8000,'software')
        sue =  Person('jack',23,10000,'hardware')
        print(bob.name,sue.salary)

        print(bob.name.split()[-1])
        bob.salary*=1.10
        print(bob.salary)

 但bob = Person('tom',22,8000,'software')
   sue =  Person('jack',23,10000,'hardware')这里报错,错误信息:

Undefined variable: Person
class Person Found at: com.onewaveinc.cas.person_start

1个回答

xiaobaiqing1983
xiaobaiqing1983   2014.09.19 14:58

应该是Person 这个名称与com.onewaveinc.cas.person_start冲突,把Person改个其它名再运行一下

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!