【HW】书本9-11至9-16选题代码和输出

9-3 用户 :创建一个名为User 的类,其中包含属性first_name 和last_name ,还有用户简介通常会存储的其他几个属性。在类User 中定义一个名为describe_user() 的方法,它打印用户信息摘要;再定义一个名为greet_user() 的方法,它向用户发出个性化的问候。创建多个表示不同用户的实例,并对每个实例都调用上述两个方法。(把User存在user.py上)
''' class user '''
class User():
	''' define User class to store user's info '''
	def __init__(self,first_name,last_name,sex,age):
		self.first_name = first_name
		self.last_name = last_name
		self.sex = sex
		self.age = age
	def describe_user(self):
		print(
			self.first_name.title() + ' ' + 
			self.last_name.title() + ' ' + 
			str(self.age) +' ' +self.sex 
			)
	def greet_user(self):
		print('Hello, '+ self.first_name)

# 9-3
from user import User

user0 = User('lily','Green','female',20)
user1 = User('mark','kim','male',19)
user2 = User('joyce','potter','female',10)
users = [user0,user1,user2]
for user in users:
	user.describe_user()
	user.greet_user()	

输出:


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。
# 9-5
class User():
	''' define User class to store user's info '''
	def __init__(self,first_name,last_name,sex,age):
		self.first_name = first_name
		self.last_name = last_name
		self.sex = sex
		self.age = age
		self.login_attempts = 0
	def describe_user(self):
		print(
			self.first_name.title() + ' ' + 
			self.last_name.title() + ' ' + 
			str(self.age) +' ' +self.sex 
			)
	def greet_user(self):
		print('Hello, '+ self.first_name)
	def increment_login_attempts(self):
		self.login_attempts += 1
	def reset_login_attempts(self):
		self.login_attempts = 0

user0 = User('lily','green','female',20)
for i in range(0,10):
	user0.increment_login_attempts()
print(user0.login_attempts)
user0.reset_login_attempts()
print(user0.login_attempts)

 


9-7 管理员 :管理员是一种特殊的用户。编写一个名为Admin 的类,让它继承你为完成练习9-3或练习9-5而编写的User 类。添加一个名为privileges 的属性,用于存储一个由字符串(如"can add post" 、"can delete post" 、"can ban user" 等)组成的列表。编写一个名为show_privileges() 的方法,它显示管理员的权限。创建一个Admin 实例,并调用这个方法。
# 9-7
from user import User

class Admin(User):
	def __init__(self,first_name,last_name,sex,age):
		super().__init__(first_name,last_name,sex,age)
		self.privileges = 'can add post'
	def show_privileges(self):
		print(self.privileges)

admin = Admin('lily','green','female',20)
admin.show_privileges()

9-8 权限 :编写一个名为Privileges 的类,它只有一个属性——privileges ,其中存储了练习9-7 所说的字符串列表。将方法show_privileges() 移到这个类中。在Admin 类中,将一个Privileges 实例用作其属性。创建一个Admin 实例,并使用方法show_privileges() 来显示其权限。
# 9-8
'''stroe class User in user.py '''
from user import User

class Privileges():
	def __init__(self):
		self.privileges = 'can add post'
	def show_privileges(self):
		print(self.privileges)


class Admin(User):
	def __init__(self,first_name,last_name,sex,age):
		super().__init__(first_name,last_name,sex,age)
		self.privileges = Privileges()

admin = Admin('lily','green','female',20)
admin.privileges.show_privileges()


9-12 多个模块 :将User 类存储在一个模块中,并将Privileges 和Admin 类存储在另一个模块(special)中。再创建一个文件,在其中创建一个Admin 实例,并对其调用方法show_privileges() ,以确认一切都依然能够正确地运行。
# 9-12

from special import Admin,Privileges

admin = Admin('lily','green','female',20)
admin.privileges.show_privileges()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值