【问题标题】:python object oriented programming type:list object is not callablepython面向对象编程类型:列表对象不可调用
【发布时间】:2018-03-15 10:51:12
【问题描述】:
class y():
    def __init__(self,favourite_channels = [""]):

        self.favourite_channels= favourite_channels
    def favourite_channels(self,i):
        print("Ur Favourite Channel=",i)
        self.favourite_channels.append(i)
x = y()
print("""*******************

1. Favourite Channel
press q to the exit
*******************""")

while True:

    a = input("Select : ")
    if a=="1":
        channels=input("Your fav channels use , to the split")
        addable_channels =channels.split(",")
        for i in addable_channels:
            x.favourite_channels(i)


    elif a=="q":
        break
    else:
        print("İnvalid Number")

【问题讨论】:

  • 请确保您的代码格式正确并提供 minimal reproducible example。此外,纯代码问题也无济于事。
  • 是的,list object is not callable 正是我期望从这段代码中得到的输出。你想发生什么?
  • 想将此 i 发送给我的 functian,然后将其添加为最喜欢的频道。它只是我代码的一部分,所以想将它添加为最喜欢的频道,我将编写 str 函数将输出我最喜欢的频道
  • 我的英语不太好 :P 对不起,顺便说一句

标签: python python-3.x function


【解决方案1】:

如果你只是在列表的末尾加上括号(x.favourite_channels 是列表),那么你会得到这样的错误。 如果你想要列表 x.favourite_channels 的第 i 个元素,你应该改写 x.favourite_channels[i-1]。

【讨论】:

    猜你喜欢
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 2014-06-03
    • 2020-05-18
    相关资源
    最近更新 更多