9-4 就餐人数/ 9-5 尝试登录次数
1. 题目

2. 代码
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = 0 # 添加属性,设置默认值为0
def describle_restaurant(self):
print(self.restaurant_name, self.cuisine_type)
def open_restaurant(self):
print("Now is opening...")
def read_restaurant(self):
print("The restaurant have " + str(self.number_served) + ' people eating.')
def set_number_served(self, member): # 添加方法,设置就餐人数
self.number_served = member
def increment_number_served(self, members): # 添加方法,设置递增人数
self.number_served += members
restaurant = Restaurant('Kaoyu',10) # 创建实例
restaurant.read_restaurant() # 初始化设置为0
restaurant.number_served = 30 # 修改值
restaurant.read_restaurant()
restaurant.set_number_served(100) # 调用方法,传值
restaurant.read_restaurant()
restaurant.increment_number_served(200)
restaurant.read_restaurant()
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
class User(): # 创建User类
def __init__(self, first_name, last_name, age, address, phone): # 属性
self.first_name = first_name
self.last_name = last_name
self.age = age
self.address = address
self.phone = phone
self.login_attempts = 0 # 添加属性
def describe_user(self): # 方法
print(self.first_name,
self.last_name,
self.age,
self.address,
self.phone)
def greet_user(self): # 方法
print("How beautiful name " + self.last_name + self.last_name,
"\n too young, too simple", "your homeland " + self.address
+ " is a warm place, ", "could you tell me your contact?")
def increment_login_attempts(self, number1): # 递增方法
self.login_attempts += number1 # 值加1
print("共登录用户量:" + str(self.login_attempts))
def reset_login_attempts(self, number2): # 重置方法,重置为0
self.login_attempts = 0
print("共登录用户量:" + str(self.login_attempts))
user = User('Michile', 'Jadon', 40, 'Chicago', 10089) # 创建实例
user.increment_login_attempts(1) #调用递增方法
user.reset_login_attempts(0) # 调用重置方法
user.increment_login_attempts(1) #调用递增
user.increment_login_attempts(1) #再次调用
user.increment_login_attempts(1) #第三次调用,此时值应该为3(每次递增1)
3. 执行结果
D:\python编程:从入门到实践\venv\Scripts\python.exe D:/python编程:从入门到实践/Restaturant02.py The restaurant have 0 people eating. The restaurant have 30 people eating. The restaurant have 100 people eating. The restaurant have 300 people eating. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 共登录用户量:1 共登录用户量:0 共登录用户量:1 共登录用户量:2 共登录用户量:3 Process finished with exit code 0

浙公网安备 33010602011771号