2 回答

TA贡献1854条经验 获得超8个赞
我试过下面的代码。
我将defrise(self,int(bonus)): 更新为defrise(self,bonus):
class Information:
def __init__(self,first,last,pay):
self.first = first
self.last = last
self.pay = pay
def rise(self,bonus):
self.pay = self.pay + bonus
def __str__(self):
return "%s and %s and has a balance of %s" % (self.first,self.last,self.pay)
if __name__ == "__main__":
emp1 = Information("tom","jerry",999)
emp1.rise(89)
print (emp1)

TA贡献1797条经验 获得超4个赞
class Information:
def __init__(self,first,last,pay):
self.first = first
self.last = last
self.pay = pay
def raise_salary(self, bonus):
self.pay += int(bonus) # exception if bonus cannot be casted
def __str__(self):
return "%s and %s and has a balance of %s" % (self.first,self.last,self.pay)
if __name__ == "__main__":
emp1 = Information("tom", "jerry", 999)
print(emp1)
emp1.raise_salary('1000') # or just emp1.raise(1000)
print(emp1)
添加回答
举报