9-5 尝试登录次数

1. 项目

在为完成练习 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

 

2. 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class User():
    """初始化用户属性"""
    def __init__(self, first_name, last_name, age, profession, login_attempts):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age
        self.profession = profession
        """添加尝试登录次数属性"""
        self.login_attempts = login_attempts
 
    def describe_user(self):
        """用户信息描述方法"""
        print("User information: " + "\n" + self.first_name.title()
              + "." + self.last_name + "\n" + str(self.age)
              + "\n" + self.profession + ".")
 
    def greet_user(self):
        """问候方法"""
        print("Hello, " + self.first_name.title() + "." + self.last_name
              + ", welcome to ShangHai.\n")
 
    def increment_login_attempts(self):
        """尝试登录次数的增量方法"""
        self.login_attempts += 1
        print(self.login_attempts)
 
    def reset_login_attempts(self):
        """重置登录次数方法"""
        self.login_attempts = 0
        print(self.login_attempts)
 
 
"""创建一个用户实例"""
user1 = User('kevin', 'hou', 30, 'EE', 10)
"""多次调用尝试登录增量方法"""
user1.increment_login_attempts()
user1.increment_login_attempts()
user1.increment_login_attempts()
"""调用重置方法"""
user1.reset_login_attempts()

  

3. 执行结果

1
2
3
4
5
6
11
12
13
0
 
Process finished with exit code 0

  

posted @   JRS077  阅读(244)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示