高级编程技术 week5 第九章

9-1餐馆:创建一个名为Restaurant 的类,其方法__init__()设置两个属性:restaurant_name 和cuisine_type。创建一个名为describe_restaurant()的方法和一个名为open_restaurant()的方法,其中前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。
class Restaurant():
    def __init__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
    def describe_restaurant(self):
        print(self.restaurant_name, " ", self.cuisine_type)
    def  open_restaurant(self):  
        print("Opening")
        
my_restaurant = Restaurant('KFC', 'fast food')
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()

9-3 用户:创建一个名为User的类,其中包含属性first_name和last_name,还有用户简介通常会存储的其他几个属性。在类User中定义一个名 为describe_user()的方法,它打印用户信息摘要;再定义一个名为greet_user()的方法,它向用户发出个性化的问候。

创建多个表示不同用户的实例,并对每个实例都调用上述两个方法。

class User():
    def __init__(self, first_name, last_name, age):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age
    def describe_user(self):
        print("User_name:",self.first_name,self.last_name, " Age:", self.age)
    def greet_user(self):
        print("Nice to meet you,",self.first_name,self.last_name)
        
user1 = User('Tom', 'Smith', 20)
user2 = User('Alice', 'Taylor', 30)
user3 = User('Bill', 'Thomas', 40)

user1.describe_user()
user1.greet_user()
user2.describe_user()
user2.greet_user()
user3.describe_user()
user3.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。

class User():
    def __init__(self, first_name, last_name, age, login_attempts = 0):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age
        self.login_attempts = login_attempts
    def describe_user(self):
        print("User_name:",self.first_name,self.last_name, " Age:", self.age)
    def greet_user(self):
        print("Nice to meet you,",self.first_name,self.last_name)
    def increment_login_attempts(self):
        self.login_attempts += 1
    def reset_login_attempts(self):
        self.login_attempts = 0
        
user1 = User('Tom', 'Smith', 20)

user1.describe_user()
user1.greet_user()

print(user1.login_attempts)
user1.increment_login_attempts()
print(user1.login_attempts)
user1.increment_login_attempts()
print(user1.login_attempts)
user1.increment_login_attempts()
print(user1.login_attempts)
user1.reset_login_attempts()
print(user1.login_attempts)


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

class User():
    def __init__(self, first_name, last_name, age, login_attempts = 0):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age
        self.login_attempts = login_attempts
    def describe_user(self):
        print("User_name:",self.first_name,self.last_name, " Age:", self.age)
    def greet_user(self):
        print("Nice to meet you,",self.first_name,self.last_name)
    def increment_login_attempts(self):
        self.login_attempts += 1
    def reset_login_attempts(self):
        self.login_attempts = 0
        
class Admin(User):
    def __init__(self, first_name, last_name, age, login_attempts = 0):
        super().__init__(first_name, last_name, age, login_attempts = 0)
        self.privileges = "can delete post"
    def show_privileges(self):
        print(self.privileges)
        
user1 = Admin('Tom', 'Smith', 20)

user1.describe_user()
user1.greet_user()

print(user1.login_attempts)
user1.increment_login_attempts()
print(user1.login_attempts)
user1.increment_login_attempts()
print(user1.login_attempts)
user1.increment_login_attempts()
print(user1.login_attempts)
user1.reset_login_attempts()
print(user1.login_attempts)

user1.show_privileges()

9-10 导入Restaurant类:将最新的Restaurant类存储在一个模块中。在另一个文件中,导入Restaurant类,创建一个Restaurant实例,并调用Restaurant的一个方法,以确认import语句正确无误。

"""restaurant.py"""
class Restaurant():
    def __init__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
    def describe_restaurant(self):
        print(self.restaurant_name, " ", self.cuisine_type)
    def  open_restaurant(self):  
        print("Opening")
from restaurant import Restaurant

my_restaurant = Restaurant('KFC', 'fast food')
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()

9-14 骰子:模块random 包含以各种方式生成随机数的函数,其中的randint()返回一个位于指定范围内的整数,例如,下面的代码返回一个1~6内的整数:

    from random import randint 

    x = randint(1, 6)

请创建一个Die类,它包含一个名为sides的属性,该属性的默认值为6。编写一个名为roll_die()的方法,它打印位于1和骰子面数之间的随机数。创建一个6面的骰子,再掷10次。

创建一个10面的骰子和一个20面的骰子,并将它们都掷10次。 

from random import randint 

class Die():
    def __init__(self, num):
        self.num = num
    def roll_die(self):
        print(randint(1,self.num))

my_die1 = Die(6);
for i in range(10):
    my_die1.roll_die()

my_die2 = Die(10);
for i in range(10):
    my_die2.roll_die()

my_die3 = Die(20);
for i in range(10):
    my_die3.roll_die()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值