初学Python 之 报错 “ TypeError: 'str' object is not callable ”

TypeError: 'str' object is not callable 


我的代码如下所示 

class Restaurant():
	'''描述餐馆的基本属性'''
	def __init__(self,restaurant_name,cuisine_type):
		self.restaurant_name = restaurant_name
		self.cuisine_type = cuisine_type

	def describe_restaurant(self):
		'''描述餐馆'''
		print("This restaurant's type is "  + self.cuisine_type + " . ")
		print("This restaurant's name is " + self.restaurant_name.title() + ".") 

	def open_restautant(self):
		'''餐馆营业提醒'''
		print("Welcom our" +  self.cuisine_type +  " " + self.restaurant_name +" restaurant !" )


restaurant_1 = Restaurant('Mandclo' ,'fast restaurant')
restaurant_2 = Restaurant('水煮鱼','中餐厅')
restaurant_3 = Restaurant('千岛酱','日本菜')


print(" name : \n " + restaurant_2.restaurant_name() )
print(" type : \n " + restaurant_2.cuisine_type() )




class Restaurant():
	'''描述餐馆的基本属性'''
	def __init__(self,restaurant_name,cuisine_type):
		self.restaurant_name = restaurant_name
		self.cuisine_type = cuisine_type

	def describe_restaurant(self):
		'''描述餐馆'''
		print("This restaurant's type is "  + self.cuisine_type + " . ")
		print("This restaurant's name is " + self.restaurant_name.title() + ".") 

	def open_restautant(self):
		'''餐馆营业提醒'''
		print("Welcom our" +  self.cuisine_type +  " " + self.restaurant_name +" restaurant !" )


restaurant_1 = Restaurant('Mandclo' ,'fast restaurant')
restaurant_2 = Restaurant('水煮鱼','中餐厅')
restaurant_3 = Restaurant('千岛酱','日本菜')


restaurant_1.describe_restaurant()
print("\n YOur dog's name is " + restaurant_1.restaurant_name + ".")

print(" name : \n " + restaurant_2.restaurant_name )
print(" type : \n " + restaurant_2.cuisine_type )

最后两行 后面的括号 不需要打,只有调用函数的时候才需要打, 不加括号是属性,所以加了括号会被报错 


红笔为错误的位置 

绿笔为 修正后 

以下为输出结果

This restaurant's type is fast restaurant . 
This restaurant's name is Mandclo.

 YOur dog's name is Mandclo.
 name : 
 水煮鱼
 type : 
 中餐厅



猜你喜欢

转载自blog.csdn.net/y912423222/article/details/81029548