python3.7程序实例_如何在Python 3.7的类中使用另一个实例变量从一个方法添加参数?...

1586010002-jmsa.png

I want to try write a code where my program will print information of the employee and then there will be another method where I can pass a parameter (integer) and that will not return anything but when I will call the method it will add that integer with one of the instance variable (in my case with self.pay). My problem is I am confused how can I call that method and so I need some help.

My code is:class Information:

def__init__ (self,first,last,pay):

self.first = first

self.last = last

self.pay = pay

def __str__(self):

return self.first + self.last + "will get" + str(self.pay)

emp1 = Information("tom","jerry",555666)

def extra(self):

self.bonus = int(self.bonus + self.pay)

calling_method = self.bonus

print (calling_method)

I would really appreciate if someone can help me out. Thank you.

What I have tried:

I have tried to write "emp1" after "extra" method but in that case I got a name error.

解决方案Try this:

class Information:

def __init__ (self,first,last,pay):

self.first = first

self.last = last

self.pay = pay

def __str__(self):

return self.first + "" + self.last + "will get " + str(self.pay)

def extra(self, value):

print("adding bonus of ", value)

self.pay += value

emp1 = Information("tom","jerry",555666)

print(emp1)

emp1.extra(111)

print(emp1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值