【问题标题】:Not sure as to where I am redefining str variable不确定我在哪里重新定义 str 变量
【发布时间】:2018-03-07 01:50:49
【问题描述】:

我目前正在学习 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

【问题讨论】:

  • 错误告诉你究竟出了什么问题。您的 self.cuisine 不是函数。只有在调用函数时才使用这些括号。只需删除它们。 self.cuisine

标签: string python-3.x class oop


【解决方案1】:

你得到了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.')

【讨论】:

    猜你喜欢
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    相关资源
    最近更新 更多