class Restaurant():#创建一个类
def __init__(self,restaurant_name,cuisine_type):#定义初始化属性,2个形参
self.restaurant_name = restaurant_name#通过self 赋值给方法
self.cuisine_type = cuisine_type
def describe_restaurant(self):#定义另一个方法
print(self.restaurant_name)
print(self.cuisine_type)
def open_restaurant(self):
print(self.restaurant_name + " is runging")
my_restaurant = Restaurant('alex','rung')#创建一个实例,虽然类没有return ,但但Python自动返回一个表示这个的实例
print(my_restaurant.restaurant_name)
print(my_restaurant.cuisine_type)
my_restaurant.describe_restaurant()#创建一个实例后,可以用.来调用方法
my_restaurant.open_restaurant()
one_restaurant = Restaurant('one','close')
one_restaurant.describe_restaurant()
two_restaurant = Restaurant('two','aaa')
two_restaurant.describe_restaurant()
shere_restaurant = Restaurant('shere','asdqwe')
shere_restaurant.describe_restaurant()
class User():
def __init__(self,first_name,ast_name,age):
self.first_name = first_name
self.ast_name = ast_name
self.abb = age
def describe_user(self):
print(self.first_name)
print(self.abb)
myname = User('alex','wei',31,)
myname.describe_user()
print(myname.first_name + " " + myname.ast_name)
'''9-4'''
class Restaurant():#创建一个餐厅的类
def __init__(self,restaurant_name,restaurant_type):#定义初始化的属性用_init_这个方法
self.restaurant_name = restaurant_name
self.restaurant_type = restaurant_type
self.number_served = 0#定义一个属性,并设置其默认值为0
def get_nuber(self):#创建一个方法打印有多少人来餐馆就餐
print(str(self.number_served ) + "个人在这个餐馆用过餐" )
def set_number_served(self,newmber):#创建一个方法,使其可以设置number_served的属性值
self.number_served = newmber
def uping_number(self,nnewmber):#创建一个方法,使其可以递增设置number_served的属性值
self.number_served += nnewmber
restaurant = Restaurant("如意餐馆",'中餐')#创建一个实例
print(str(restaurant.number_served) + "个人在这个餐馆用过餐")
restaurant.number_served = 5#调用类属性修改属性的值
print(str(restaurant.number_served) + "个人在这个餐馆用过餐")
restaurant.set_number_served(25)#调用类方法修改属性的值
restaurant.get_nuber()#调用方法使其得到就餐人数
restaurant.uping_number(25)
restaurant.get_nuber()
'''9-5'''
class User():
def __init__(self,user_name,user_age):
self.user_name = user_name
self.user_age = user_age
self.login_attempts = 0
def increment_login_attempts(self):#定义一个方法让属性累加
self.login_attempts += 1
def reset_attemptss(self):#定义一个方法让属性归0
self.login_attempts = 0
def get_user_login_attempts_print(self):
print("该用户登录的次数为第" + str (self.login_attempts) + "次")
def get_user_login_attempts(self):
print(self.login_attempts)
user = User('a',12)
for i in range(1,51):#用for 循环让累加的方法执行50次
user.increment_login_attempts()
user.get_user_login_attempts_print()
Active = True
while Active == True:#用while循环让用户自主选择是否要清除记录
cl = input("该用户登录" + str(user.login_attempts) + "次,是否需要清除Y/N")
if cl.lower() == 'y':#判断是否需要清除
user.reset_attemptss()#调用类清除的方法
user.get_user_login_attempts_print()
break#清除后跳出循环
else:
cm = input("用户记录未清除,登录次数为" + str(user.login_attempts) + "次,是否继续清除Y/N" )
if cm.lower() == 'y':
continue#如果用户选择继续清除重新开始上面的while判断
else:
Active = False
'''9-6'''
class Restaurant():#创建一个餐厅的类
def __init__(self,restaurant_name,restaurant_type):#定义初始化的属性用_init_这个方法
self.restaurant_name = restaurant_name
self.restaurant_type = restaurant_type
self.number_served = 0#定义一个属性,并设置其默认值为0
def get_resinfo(self):
print("餐厅的名称是:" + self.restaurant_name)
print("餐厅的类型是:" + self.restaurant_type)
def get_nuber(self):#创建一个方法打印有多少人来餐馆就餐
print(str(self.number_served ) + "个人在这个餐馆用过餐" )
def set_number_served(self,newmber):#创建一个方法,使其可以设置number_served的属性值
self.number_served = newmber
def uping_number(self,nnewmber):#创建一个方法,使其可以递增设置number_served的属性值
self.number_served += nnewmber
class IceCreamstand(Restaurant):#创建一个子类,继承父类的属性和方法
def __init__(self,restaurant_name,restaurant_type):#初始化父类的形参
super().__init__(restaurant_name,restaurant_type)#用super()函数关联父类的方法和属性
self.flavors = ['草莓','西瓜','原味']#在子类中新增一个属性
def ice_print(self):#在子类中创建一个打印的方法
print("冰淇淋的口味有" + str(self.flavors[:]) + "!")
myice = IceCreamstand('我的餐厅','甜品')
myice.get_resinfo()
myice.ice_print()
'''9-14'''
from random import randint
class Die():
def __init__(self,):
self.sides = 6
def roll_die(self):
print(randint(1,self.sides))
die = Die()
die.roll_die()
print("\n")
die6 = Die()
die6.sides = 10
x = 0
for i in range(0,10):
die6.roll_die()
x +=1
print("\n")
print(x)
print("\n")
die20 = Die()
die20.sides = 20
x = 0
for i in range(0,10):
die20.roll_die()
x+=1
print("\n")
print(x)