【Python】9-5 课后习题解题代码—Python编程从入门到实践(个人复习)

  • Post author:
  • Post category:python





1 问题

在这里插入图片描述





1

 问题

9

5

图1~ 问题9-5









1




问题


9













5







2 代码及其输出

# 9-5
class User:
    def __init__(self, first_name, last_name, login_attempts):
        self.first_name = first_name
        self.last_name = last_name
        self.login_attempts = login_attempts

    def describe_user(self):
        print("user name is " + self.first_name + self.last_name)

    def greet_user(self):
        print("hello! " + self.first_name + self.last_name)

    def increment_login_attempts(self):
        self.login_attempts += 1

    def reset_login_attempts(self):
        self.login_attempts = 0

# Answer question 9-5!

# create two objects
u_1 = User("k", "t4ngw", 0)


# Calling 5 times!
for i in range(5):
    u_1.increment_login_attempts()
print(u_1.login_attempts) # login_attempts = 5

# reset login_attempts!
u_1.reset_login_attempts()
print(u_1.login_attempts) # login_attempts = 0
# output
5
0



版权声明:本文为t4ngw原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。