python-16-零基础学python 用类实现登录次数的记录

学习内容:《python编程:从入门到实践》第二版

知识点:

类,特殊函数,编写方法,创建实例,用方法修改类的值

练习内容:

练习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。

我的代码&运行结果:

python 复制代码
class User:
    def __init__ (self,first_name,last_name,age,job,login_attempts):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age
        self.job = job
        self.login_attempts = login_attempts
 
    def describe_user(self):
        print(f"{self.first_name}{self.last_name}\t{self.age}岁\t{self.job}\n登录尝试:{self.login_attempts}次")
 
    def greet_user(self):
        print(f"hello,{self.first_name}{self.last_name},welcome!")

    def increment_login_attempts(self):
        self.login_attempts += 1
        
    def reset_login_attempts(self):
        self.login_attempts = 0
        
 
user1 = User('张','三','18','学生',0)
user1.describe_user()
user1.greet_user()

user1.increment_login_attempts()
user1.increment_login_attempts()
user1.increment_login_attempts()
user1.increment_login_attempts()
user1.increment_login_attempts()
user1.increment_login_attempts()
user1.describe_user()

print("系统重置")
user1.reset_login_attempts()
user1.describe_user()

注意点:

新增的属性要做好与类的关联:self.login_attempts = login_attempts

可以再加一个方法,单独打印尝试登录次数

总结&问题:

这一部分可以用在登录账号,输入密码,密码次数

智能家居每天密码锁开了几次

等等~

累计次数、重置等都可以用。

基础的东西也很实用

相关推荐
m0_738120723 分钟前
网络安全编程——Python编写Python编写基于UDP的主机发现工具(完结:解码ICMP头)
python·网络协议·安全·web安全·udp
知白守黑V8 分钟前
从源码看 Flocks:一个 AI 原生 SecOps 平台应该长什么样
python·web安全·安全威胁分析·数据安全·安全运营·ai安全·flocks
m0_7164300710 分钟前
如何在MongoDB中监控集群中的僵尸连接_释放长时间不活跃的游标资源
jvm·数据库·python
m0_7164300710 分钟前
HTML函数开发最低配置是多少_HTML函数入门硬件门槛【指南】
jvm·数据库·python
zzwq.20 分钟前
时间序列处理:日期解析与重采样
python·数据分析
Wyz2012102429 分钟前
SQL中如何处理GROUP BY的不可排序问题_ORDERBY与聚合
jvm·数据库·python
Java面试题总结32 分钟前
Python 入门(四)- Openpyxl 操作 Excel 教程
开发语言·python·excel
Polar__Star35 分钟前
jsoup如何读取html
jvm·数据库·python
smj2302_7968265236 分钟前
解决leetcode第3901题好子序列查询
python·算法·leetcode
a95114164239 分钟前
怎么防范通过phpMyAdmin上传WebShell_禁止into outfile权限
jvm·数据库·python