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 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律