我是 Python 的初学者,刚刚开始学习课程。我敢肯定这可能是非常基本的东西,但为什么这段代码:
class Television():
def __init__(self):
print('Welcome your TV.')
self.volume = 10
self.channel = 1
def channel(self, channel):
self.channel = input('Pick a channel: ')
print('You are on channel ' + self.channel)
def volume_up(self, amount):
self.amount = ('Increase the volume by: ')
self.volume += self.amount
print('The volume is now ' + self.volume)
def volume_down(self, amount):
self.amount = ('Decrease the volume by: ')
self.volume -= self.amount
print('The volume is now ' + self.volume)
myTele = Television()
myTele.channel()
myTele.volume_up()
myTele.volume_down()
产生以下错误:
TypeError: 'int' object is not callable, Line 18
编辑:我将代码更改为:
class Television():
def __init__(self, volume = 10, channel = 1):
print('Welcome your TV.')
self.volume = volume
self.channel = channel
def change(self, channel):
self.channel = input('Pick a channel: ')
print('You are on channel ' + self.channel)
def volume_up(self, amount):
self.amount = int(input('Increase the volume by: '))
self.volume += self.amount
print('The volume is now ' + str(self.volume))
def volume_down(self, amount):
self.amount = int(input('Decrease the volume by: '))
self.volume -= self.amount
print('The volume is now ' + str(self.volume))
myTele = Television()
myTele.change()
myTele.volume_up()
myTele.volume_down()
但它返回:
TypeError: change() missing 1 required positional argument: 'channel'
同样,这是来自刚开始上课的人,所以如果我做了一些明显错误的事情,请不要太苛刻。谢谢你。