练习9.5_尝试登陆次数_Python编程:从入门到实践(第3版)

在为练习9.3编写的User类中,添加一个名为login_attempts的属性。编写一个名为

increment_login_attempts()的方法,用来将属性login_attempts的值加1。再编写一个

名为reset_login_attempts()的方法,用来将属性login_attempts的值重置为0。

根据User类创建一个实例,再调用increment_login_attempts()方法多次。打印属性

login_attempts的值,确认它正确地递增了。然后,调用方法reset_login_attempts(),

并再次打印属性login_attempts的值,确认它被重置为0。

class User:
    def __init__(self, last_name, first_name, location, gender):
        self.first_name = first_name
        self.last_name = last_name
        self.location = location
        self.gender = gender
        self.login_attempts = 0

    def describe_user(self):
        print(f'姓:{self.last_name},名:{self.first_name},地点:{self.location},性别:{self.gender}')

    def greet_user(self):
        print(f'{self.last_name}{self.first_name},欢迎!')

    def increment_login_attempts(self):
        self.login_attempts += 1

    def reset_login_attempts(self):
        self.login_attempts = 0


user1 = User('王', '二', '上海', '男')
user1.increment_login_attempts()
user1.increment_login_attempts()
user1.increment_login_attempts()
user1.increment_login_attempts()
print(f'登陆已经尝试{user1.login_attempts}次')
user1.reset_login_attempts()
print(f'登陆已经尝试{user1.login_attempts}次')

输出:

登陆已经尝试4次
登陆已经尝试0次

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值