《Python编程:从入门到实践》答案
i人生总会有让你不舒服的事,不要在它上纠结,把时间浪费在你想做的事上。
第九章
class Restaruarant():
def __init__(self,restaurant_name,cuisine_type):
self.restaurant_name=restaurant_name
self.cuisine_type=cuisine_type
def describe_restaurant(self):
print("The cuisine_type is: "+self.cuisine_type)
print("The restaurant_name is: "+self.restaurant_name)
def open_restaurant(self):
print("We are in business.")
R1=Restaruarant('客来','东北菜')
print(R1.restaurant_name+" is good at "+R1.cuisine_type+".")
R1.describe_restaurant()
R1.open_restaurant()
出错点:
1、__init__()两边的下划线是两个!
2、两个方法里忘记了加self
class Restaruarant():
def __init__(self,restaurant_name,cuisine_type):
self.restaurant_name=restaurant_name
self.cuisine_type=cuisine_type
def describe_restaurant(self):
print("The cuisine_type is: "+self.cuisine_type)
print("The restaurant_name is: "+self.restaurant_name)
def open_restaurant(self):
print("We are in business.")
R1=Restaruarant('客来','东北菜')
print(R1.restaurant_name+" is good at "+R1.cuisine_type+".")
R1.describe_restaurant()
R1.open_restaurant()
print("\n")
R2=Restaruarant('红旗','东北菜')
R3=Restaruarant('山西','刀削面')
R4=Restaruarant('西北','兰州拉面')
R2.describe_restaurant()
R2.open_restaurant()
R3.describe_restaurant()
R3.open_restaurant()
R4.describe_restaurant()
R4.open_restaurant()
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("first name is "+self.first_name)
print("last name is "+self.last_name)
print("age is "+str(self.age))
def greet_user(self):
full_name=self.first_name+" "+self.last_name
print("Welcome you ,"+full_name+" ,have a nice day .")
user1=User('张','宸宸',23)
user1.greet_user()
user1.describe_user()
user2=User('王','舍',24)
user2.greet_user()
user2.describe_user()
user3=User('肖','大在',83)
user3.greet_user()
user3.describe_user()
【注意】每个属性前面都是要加self.的。
class Restaruarant():
def __init__(self,restaurant_name,cuisine_type):
self.restaurant_name=restaurant_name
self.cuisine_type=cuisine_type
self.number_served=0
def describe_restaurant(self):
print("The cuisine_type is: "+self.cuisine_type)
print("The restaurant_name is: "+self.restaurant_name)
def open_restaurant(self):
print("We are in business.")
def set_number_served(self,num):
self.number_served=num
def increment_number_served(self,num):
self.number_served+=num
R1=Restaruarant('客来','东北菜')
print(R1.restaurant_name+" is good at "+R1.cuisine_type+
"and "+str(R1.number_served)+" people have been came here.")
R1.set_number_served(3000)
print(R1.restaurant_name+" is good at "+R1.cuisine_type+
"and "+str(R1.number_served)+" people have been came here.")
R1.increment_number_served(30)
print(R1.restaurant_name+" is good at "+R1.cuisine_type+
"and "+str(R1.number_served)+" people have been came here.")
class User():
def __init__(self,first_name,last_name,age):
self.first_name=first_name
self.last_name=last_name
self.age=age
self.login_attempts=0
def increment_login_attempts(self):
self.login_attempts+=1
def reset_login_attempts(self):
self.login_attempts=0
def describe_user(self):
print("first name is "+self.first_name)
print("last name is "+self.last_name)
print("age is "+str(self.age))
def greet_user(self):
full_name=self.first_name+" "+self.last_name
print("Welcome you ,"+full_name+" ,have a nice day .")
user1=User('张','宸宸',23)
print(user1.login_attempts)
user1.increment_login_attempts();
user1.increment_login_attempts();
user1.increment_login_attempts();
user1.increment_login_attempts();
print(user1.login_attempts)
user1.reset_login_attempts();
print(user1.login_attempts)
class Restaruarant():
def __init__(self,restaurant_name,cuisine_type):
self.restaurant_name=restaurant_name
self.cuisine_type=cuisine_type
self.number_served=0
def describe_restaurant(self):
print("The cuisine_type is: "+self.cuisine_type)
print("The restaurant_name is: "+self.restaurant_name)
def open_restaurant(self):
print("We are in business.")
def set_number_served(self,num):
self.number_served=num
def increment_number_served(self,num):
self.number_served+=num
class IceCreamStand(Restaruarant):
def __init__(self,restaurant_name,cuisine_type):
super().__init__(restaurant_name,cuisine_type)
self.flavors=['strawberry','berry','grape','banana']
def show_Ice(self):
for key in self.flavors:
print(key)
My_Ice=IceCreamStand('客来','东北菜')
My_Ice.show_Ice()
class User():
def __init__(self,first_name,last_name,age):
self.first_name=first_name
self.last_name=last_name
self.age=age
self.login_attempts=0
def increment_login_attempts(self):
self.login_attempts+=1
def reset_login_attempts(self):
self.login_attempts=0
def describe_user(self):
print("first name is "+self.first_name)
print("last name is "+self.last_name)
print("age is "+str(self.age))
def greet_user(self):
full_name=self.first_name+" "+self.last_name
print("Welcome you ,"+full_name+" ,have a nice day .")
class Admin(User):
def __init__(self,first_name,last_name,age):
super().__init__(first_name,last_name,age)
self.privileges=["can ban user",'can delete post','can add post']
def show_privileges(self):
for key in self.privileges:
print(key)
Admin1=Admin('张','宸宸',23)
Admin1.show_privileges()
【注意】super().__init__()的写法。、
class Privileges():
def __init__(self):
self.privileges=["can ban user",'can delete post','can add post']
def show_privileges(self):
for key in self.privileges:
print(key)
class User():
def __init__(self,first_name,last_name,age):
self.first_name=first_name
self.last_name=last_name
self.age=age
self.login_attempts=0
def increment_login_attempts(self):
self.login_attempts+=1
def reset_login_attempts(self):
self.login_attempts=0
def describe_user(self):
print("first name is "+self.first_name)
print("last name is "+self.last_name)
print("age is "+str(self.age))
def greet_user(self):
full_name=self.first_name+" "+self.last_name
print("Welcome you ,"+full_name+" ,have a nice day .")
class Admin(User):
def __init__(self,first_name,last_name,age):
super().__init__(first_name,last_name,age)
self.privileges=Privileges()
Admin1=Admin('张','宸宸',23)
Admin1.privileges.show_privileges()
【注意】
1、把某个函数抽取出来之后要把原函数删除。
2、当把一个类的实例用作了属性时,调用这个成为属性的类中的方法要用【类 . 属性名 . 被当成属性的类的方法】而不能直接由类调用被当成属性的类的方法。
3、被当成属性的类要写在最上边,先加载。
class Car():
def __init__(self,made,model,year):
self.made=made
self.model=model
self.year=year
def descriptive_name(self):
long_name=str(self.year)+' '+self.made+' '+self.model
return long_name.title()
class Battery():
def __init__(self,battery=70):
self.battery=battery
def describe_battery(self):
print("This car has a "+str(self.battery)+"-kwh battery")
def get_range(self):
if self.battery==70:
range=240
elif self.battery==85:
range=270
message="This car can go "+str(range)
message+=" miles on a full charge."
print(message)
def update_battery(self):
if self.battery!=85:
self.battery=85
class EleCar(Car):
def __init__(self,made,model,year):
super().__init__(made,model,year)
self.battery=Battery()
new_eleCar=EleCar("Audi","s450",4)
new_eleCar.battery.get_range()
new_eleCar.battery.update_battery()
new_eleCar.battery.get_range()
#Restaurant.py
class Restaurant():
def __init__(self,name,make,model,year):
self.name=name
self.make=make
self.model=model
self.year=year
def show_detils(self):
print(str(self.year)+" , "+self.name+" , "+self.model+" , "+self.make)
from Restaurant import Restaurant
my_class=Restaurant('Benz','China','s-450','2016')
my_class.show_detils()
#模块
class Privileges():
def __init__(self):
self.privileges=["can ban user",'can delete post','can add post']
def show_privileges(self):
for key in self.privileges:
print(key)
class User():
def __init__(self,first_name,last_name,age):
self.first_name=first_name
self.last_name=last_name
self.age=age
self.login_attempts=0
def increment_login_attempts(self):
self.login_attempts+=1
def reset_login_attempts(self):
self.login_attempts=0
def describe_user(self):
print("first name is "+self.first_name)
print("last name is "+self.last_name)
print("age is "+str(self.age))
def greet_user(self):
full_name=self.first_name+" "+self.last_name
print("Welcome you ,"+full_name+" ,have a nice day .")
class Admin(User):
def __init__(self,first_name,last_name,age):
super().__init__(first_name,last_name,age)
self.privileges=Privileges()
from ex911 import User,Privileges,Admin
Admin1=Admin('张','宸宸',23)
Admin1.privileges.show_privileges()
from ex913 import User
class Privileges():
def __init__(self):
self.privileges=["can ban user",'can delete post','can add post']
def show_privileges(self):
for key in self.privileges:
print(key)
class Admin(User):
def __init__(self,first_name,last_name,age):
super().__init__(first_name,last_name,age)
self.privileges=Privileges()
class User():
def __init__(self,first_name,last_name,age):
self.first_name=first_name
self.last_name=last_name
self.age=age
self.login_attempts=0
def increment_login_attempts(self):
self.login_attempts+=1
def reset_login_attempts(self):
self.login_attempts=0
def describe_user(self):
print("first name is "+self.first_name)
print("last name is "+self.last_name)
print("age is "+str(self.age))
def greet_user(self):
full_name=self.first_name+" "+self.last_name
print("Welcome you ,"+full_name+" ,have a nice day .")
from ex911 import Privileges,Admin
from ex913 import User
Admin1=Admin('张','宸宸',23)
Admin1.privileges.show_privileges()
from collections import OrderedDict
#创建三个同学,把他们放在一个班级里,遍历出所有同学,
#错误1 把班级写成了class,整成了Python中的关键字。
#错误2 内部的字典最后一个键值对后面也是有逗号的。
#错误3 “good at ”在字典中写的时候后面是有空格的,但for 循环中忘记写了。
classes = OrderedDict()
classes['s1']={
'name':'Zhang',
'num':'001',
'age':'18',
'good at ':'math',
}
classes['s2']={
'name':'Wang',
'num':'002',
'age':'16',
'good at ':'Chinese',
}
classes['s3']={
'na3e':'Li',
'num':'003',
'age':'17',
'good at ':'Python',
}
for name,info in classes.items():
print('\nStudent name is : '+name)
print('\tStudent number is : '+info['num'])
print('\tStudent age is : '+info['age'])
print('\tStudent goot at is : '+info['good at '])
from random import randint
class Die():
def __init__(self):
self.sides=6
def show_die(self):
self.sides=randint(1,6)
print(self.sides)
ex=Die()
for i in range(1,11):
ex.show_die()
class Die2():
def __init__(self):
self.sides=6
def show_die(self):
self.sides=randint(1,11)
print(self.sides)
ex2=Die2()
for i in range(1,11):
ex2.show_die()
还没有评论,来说两句吧...