第九章课后习题

9-3:

class User():
    def __init__(self,first_name,last_name,gender,age):
        self.first_name=first_name
        self.last_name=last_name
        self.gender=gender
        self.age=age
    def describe_user(self):
        print("Name: "+self.first_name.title()+" "+self.last_name.title()+";")
        print("Gender: "+self.gender+";")
        print("Age: "+self.age+";")
    def greet_user(self):
        print("Hello, "+self.first_name.title()+self.last_name.title()+".")

Shizhang=User('Zhan','Jianzhou','Male','30')
Shizhang.describe_user()
Shizhang.greet_user()

9-5:

class User():
    def __init__(self,first_name,last_name,gender,age):
        self.first_name=first_name
        self.last_name=last_name
        self.gender=gender
        self.age=age
        self.login_attempts=0
    def describe_user(self):
        print("Name: "+self.first_name.title()+" "+self.last_name.title()+";")
        print("Gender: "+self.gender+";")
        print("Age: "+self.age+";")
    def greet_user(self):
        print("Hello, "+self.first_name.title()+self.last_name.title()+".")
    def showLogin_attempts(self):
        print("The login attemps is "+str(self.login_attempts)+".")
    def increment_login_attempts(self):
        self.login_attempts=self.login_attempts+1
    def reset_login_attempts(self):
        self.login_attempts=0

Shizhang=User('Zhan','Jianzhou','Male','30')
Shizhang.showLogin_attempts()
Shizhang.increment_login_attempts()
Shizhang.increment_login_attempts()
Shizhang.showLogin_attempts()
Shizhang.reset_login_attempts()
Shizhang.showLogin_attempts()

9-7:

class User():
    def __init__(self,first_name,last_name,gender,age):
        self.first_name=first_name
        self.last_name=last_name
        self.gender=gender
        self.age=age
        self.login_attempts=0
    def describe_user(self):
        print("Name: "+self.first_name.title()+" "+self.last_name.title()+";")
        print("Gender: "+self.gender+";")
        print("Age: "+self.age+";")
    def greet_user(self):
        print("Hello, "+self.first_name.title()+self.last_name.title()+".")
    def showLogin_attempts(self):
        print("The login attemps is "+str(self.login_attempts)+".")
    def increment_login_attempts(self):
        self.login_attempts=self.login_attempts+1
    def reset_login_attempts(self):
        self.login_attempts=0
class Admin(User):
    def __init__(self,first_name,last_name,gender,age,privileges):
        super().__init__(first_name,last_name,gender,age)
        self.privileges=privileges
    def show_privileges(self):
        print(self.privileges)

Shizhang=Admin('Zhan','Jianzhou','Male','30',['can delete post','can add post'])
Shizhang.show_privileges()

9-11:

from admin import Admin
Shizhang=Admin('Zhan','Jianzhou','Male','30',['can delete post','can add post'])
Shizhang.show_privileges()

猜你喜欢

转载自blog.csdn.net/qq_39178023/article/details/79696459