9-1
classRestaurant():def __init__(self,restaurant_name,cuisine_type):
self.restaurant_name=restaurant_name
self.cuisine_type=cuisine_typedefdescribe_restaurant(self):print(self.restaurant_name.title())print(self.cuisine_type.title())defopen_restaurant(self):print("The restaurant is opening")
restaurant= Restaurant(‘quanjude‘,‘kaoya‘)
restaurant.describe_restaurant()
restaurant.open_restaurant()
9-2略
9-3 数字实参不加引号 好像是这样的。。因为报了错
classUser():def __init__(self, first_name, last_name,age):
self.first_name=first_name
self.last_name=last_name
self.age=agedefdescribe_user(self):print(‘Name:‘ +self.last_name + self.first_name+ ‘age :‘ +str(self.age) )defgreet_user(self):print(‘Hello!‘ + self.last_name +self.first_name)
user_a= User(‘xiangnai‘, ‘taonaimu‘,‘22‘)
user_b= User(‘yongmei‘, ‘shentian‘,‘23‘)
user_a.describe_user()
user_b.describe_user()
user_a.greet_user()
user_b.greet_user()
9-4 写95的时候发现这个题没看到后面的 不过和95差不多不补了
classRestaurant():def __init__(self,restaurant_name,cuisine_type):
self.restaurant_name=restaurant_name
self.cuisine_type=cuisine_type
self.number_served=0defdescribe_restaurant(self):print(self.restaurant_name.title())print(self.cuisine_type.title())defopen_restaurant(self):print("The restaurant is opening")defread_serve_number(self):print("The numer restanrant serverd is" +str(self.number_served))
restaurant= Restaurant(‘restaurant‘,‘aaa‘)
restaurant.describe_restaurant()
restaurant.read_serve_number()
restaurant.number_served= 20restaurant.read_serve_number()
输出:
Restaurant
Aaa
The numer restanrant serverdis0
The numer restanrant serverdis 20
9-5
classUser():def __init__(self, first_name, last_name,age):
self.first_name=first_name
self.last_name=last_name
self.age=age
self.login_attemps=0defdescribe_user(self):print(‘Name:‘ +self.last_name + self.first_name+ ‘age :‘ +str(self.age) )defgreet_user(self):print(‘Hello!‘ + self.last_name +self.first_name)defincrement_login_attemps(self):
self.login_attemps+= 1
defread_login(self):print("Now the number of people logining is" +str(self.login_attemps))defreset_login(self):
self.login_attemps=0
user_a= User(‘san‘,‘zhang‘,‘20‘)
user_a.increment_login_attemps()
user_a.increment_login_attemps()
user_a.increment_login_attemps()
user_a.increment_login_attemps()
user_a.read_login()
user_a.reset_login()
user_a.read_login()
输出:
Now the number of people loginingis 4Now the number of people loginingis 0
9-6
classRestaurant():def __init__(self,restaurant_name,cuisine_type):
self.restaurant_name=restaurant_name
self.cuisine_type=cuisine_typedefdescribe_restaurant(self):print(self.restaurant_name.title())print(self.cuisine_type.title())defopen_restaurant(self):print("The restaurant is opening")classIceCreamStand(Restaurant):def __init__(self,restaurant_name,cuisine_type):
super().__init__(restaurant_name,cuisine_type)
self.flavors=[]defset_flavors(self,flavors):
self.flavors=flavorsdefdescribe_flavors(self):print("This is the flavors:" +str(self.flavors))#这里一开始没加str报错 can only concatenate str (not "list") to str 不懂为啥
Newres= IceCreamStand(‘quanjude‘,‘kaoya‘)
Newres.set_flavors([‘apple‘,‘orange‘,‘banana‘])
Newres.describe_flavors()
输出:
Thisis the flavors:[‘apple‘, ‘orange‘, ‘banana‘]
9-7 看别人的学了一手 end=‘‘, 可以继续后面print的内容 挺好用
classUser():def __init__(self, first_name, last_name,age):
self.first_name=first_name
self.last_name=last_name
self.age=age
self.login_attemps=0defdescribe_user(self):print(‘Name:‘ +self.last_name + self.first_name+ ‘age :‘ +str(self.age) )defgreet_user(self):print(‘Hello!‘ + self.last_name +self.first_name)defincrement_login_attemps(self):
self.login_attemps+= 1
defread_login(self):print("Now the number of people logining is" +str(self.login_attemps))defreset_login(self):
self.login_attemps=0classAdmin(User):def __init__(self, first_name, last_name, age):
super().__init__(first_name, last_name, age)
self.privileges= [‘can add post‘, ‘can delete post‘, ‘can ban user‘]defshow_privileges(self):print(‘管理员权限有:‘, end=‘‘)for privilege inself.privileges:print(privilege + ‘,‘, end=‘ ‘)
admin= Admin(‘San‘, ‘Zhang‘, ‘20‘)
admin.show_privileges()
输出:
管理员权限有:can add post, can delete post, can ban user,
9-8 略 编写一个类,然后admin中 self.pr... = pr...()
9-9
defupgrade_battery(self):if self.battery_size!=85:
self.battery_size=85
9-14
from random importrandintclassDie():def __init__(self,sides=6):
self.sides=sidesdefroll_die(self):print(randint(1,self.sides))
die_six= Die(6)for i in range(10):
die_six.roll_die()
die_ten= Die(10)for i in range(10):
die_ten.roll_die()
die_20= Die(20)for i in range(10):
die_20.roll_die()
有点3分钟的感觉了。。要认真一点。
原文地址:https://www.cnblogs.com/zhangyueba/p/12249247.html