《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(
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

纸城

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值