channel()缺少1个位置参数

时间:2017-12-25 06:36:22

标签: python python-3.x

class Television(object):

    def __init__(self, lst):
        self.lst = lst

    def channel(self, number):
        print("You are currently tuning into" + self.lst[number-1])


def volume(reduce, loudness=0):
    loudness -= reduce
    return loudness


def main():
    channel = ['News','Sport','Movie','Music','Kids']
    TV = Television(channel)
    numbers = int(input("What do u want to watch?"))
    watch = Television.channel(numbers)
    reduce = int(input("Too loud? Reduce volume!"))
    adjust = Television(reduce)

main()

input("Press enter to exit")

如上面的代码所示,channel方法只需要1个参数,即数字。但是,当我调出Television.channel(numbers)时,其中numbers是用户输入的值,它会返回标题中显示的以下错误。我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:0)

您需要在实例channel()上调用TV方法:

class Television(object):
    def __init__(self, lst):
        self.lst = lst

    def channel(self, number):
        print("You are currently tuning into " + self.lst[number-1])

def main():
    channels = ['News', 'Sport', 'Movie', 'Music', 'Kids']
    TV = Television(channels)
    number = int(input("What do u want to watch? "))
    watch = TV.channel(number)    

main()

input("Press enter to exit ")

运行上面的代码:

What do u want to watch? 3
You are currently tuning into Movie
Press enter to exit 
>>>