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 randintx = 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()
