Python练习题 9-5尝试登录次数

来源:互联网 发布:3d打印机切片软件 编辑:程序博客网 时间:2024/06/05 02:30

9-5 尝试登录次数:在为完成练习 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,first_name,last_name):        self.first_name=first_name        self.last_name=last_name        self.login_attempts=0    def describe_user(self):        print(self.first_name+self.last_name)    def greet_user(self):        print("hey,tony.")    def increment_login_attempts(self):        self.login_attempts+=1    def reset_login_attempts(self):        self.login_attempts=0Tony=User('Tony','Stark')Tony.describe_user() for n in range(5):    Tony.increment_login_attempts()print(Tony.login_attempts)Tony.reset_login_attempts() print(Tony.login_attempts)
原创粉丝点击