不确定我在哪里重新定义str变量

时间:2018-03-07 01:50:49

标签: string python-3.x class oop

我目前正在学习python的基础知识,我正处于重度学习的第二天,我无法看到我试图调用字符串的位置:margaret_order.cuisine_style()。任何帮助将不胜感激。

class Restaurant():
    '''Create simple restaurant details.'''

    def __init__(self,name,cuisine):
        '''Initialize name and cuisine type attributes.'''
        self.name = name
        self.cuisine = cuisine

    def print_order(self):
        """Define individual who is ordering"""
        print (self.name.title() + ' is the next individual to order.')

    def cuisine_style(self):
        '''Define the dish that the individual wants to order'''
        print (self.cuisine() + ' Is the type of food that was ordered.')

jimmies_order = Restaurant('Jimmie','Fish Sandwhich')
margaret_order = Restaurant('Margaret','Lasagna')

margaret_order.cuisine_style()

我得到的错误是:

  File "/Users/admin/Library/Preferences/PyCharmCE2017.3/scratches/scratch_3.py", line 15, in cuisine_style
    print (self.cuisine() + ' Is the type of food that was ordered.')
TypeError: 'str' object is not callable

1 个答案:

答案 0 :(得分:0)

您收到TypeError,因为在第15行(print (self.cuisine() + ' Is the type of food that was ordered.'))中,您在self.cuisine之后添加了一组括号,这意味着您正在尝试调用字符串,这是不可能的。要解决此问题,请使用以下行:

print (self.cuisine + ' Is the type of food that was ordered.')