您的位置:首页 > 编程语言 > Python开发

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

2017-06-02 23:25 2731 查看
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=0

Tony=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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: