关于电视模拟的类模板存在巨大问题
大家好,问题来了。
我正在开发一个与类方法相关的项目来模拟电视菜单。
如果输入0,则应该退出main函数。如果输入 1,则应关闭电视并显示一条消息,说明电视是否已打开或关闭。如果输入 2,则必须输入大于 0 且小于 500 的频道编号才能切换到该频道。如果输入 3,则应提高音量,而输入 4 应降低音量,并显示文本告诉您音量更改是否成功。另外,音量不能低于 0 或高于 10。
但是除了选项 0 和 3 之外的所有选项都存在严重问题。
当我输入 2 尝试设置通道时,它什么也不显示。
当我输入 4 时,显示了文本,但音量本身没有改变,无论如何始终保持在 5。
当我输入1时,总是显示电视已关闭,从不显示电视已打开。但是当我输入 3 时,音量仍然在成功增加,而它应该失败并显示描述音量增加应该失败的文本。
这是代码:
class Television:
"""Get yourself some televisions"""
def __init__(self, channel, volume, is_on):
self.channel = channel
self.volume = volume
self.is_on = is_on
is_on = False
def __str__(self):
string = "\nStatus: "
string += "\nChannel " + self.channel
string += "\nVolume" + self.volume
if is_on == True:
string += "\n" + self.is_on
return string
def toggle_power(self):
if self.is_on == True:
self.is_on += False
print("The TV has been turned off.")
if self.is_on == False:
self.is_on += True
print("The TV has been turned on.")
def get_channel(self):
print("You're currently in channel", self.channel)
return self.channel
def set_channel(self):
if self.is_on == True:
self.channel = float(input("Enter the channel number you want to switch to."))
if current < 500 and current > 0:
print("You have been switched to channel", self.channel)
else:
print("Channel not found.")
elif self.is_on == False:
print("The TV has been turned off.")
def raise_volume(self):
if self.is_on == True:
if self.volume < 10:
self.volume += 1
print("The volume has been successfully increased. Your current volume is", self.volume)
else:
print("The attempt to raise the volume level was unsuccessful.")
elif self.is_on == False:
print("The TV has been turned off.")
def lower_volume(self):
if self.is_on == True:
if self.volume > -1:
self.volume +- 1
print("The volume has been successfully lowered. Your current volume is", self.volume)
else:
print("The attempt to lower the volume level was unsuccessful.")
elif self.is_on == False:
print("The TV has been turned off.")
def main():
choice = 1
tv1 = Television(1, 5, True)
while choice != 0:
choice = int(input("""0 - Exit
1 - Toggle Power
2 - Change Channel
3 - Raise Volume
4 - Lower Volume
"""))
if choice == 1:
tv1.toggle_power()
if choice == 2:
tv1.get_channel
tv1.set_channel
if choice == 3:
tv1.raise_volume()
if choice == 4:
tv1.lower_volume()
main()
这些问题非常严重......请告诉我是否有办法解决它们!
Good day everyone, so here's the problem.
I was working on a class method related project to simulate a Television menu.
If you enter 0, it should exit the main function. If you enter 1, it should power off the Television and display a message saying whether or not the TV has been turned on or off. If you enter 2, you'll have to enter a channel number that are bigger than 0 and less than 500 in order to switch into that channel. If you enter 3 you should raise your volume, while entering 4 should lower the volume with texts displaying telling you if the volume change has succeeded or not. Also, the volume can't be lower than 0 or higher than 10.
But everything aside from option 0 and 3 is having severe issues.
When I enter 2 to try to get the channels set up, it displays nothing.
When I enter 4, the texts were displayed but the volume itself didn't change and always stays in 5 no matter what.
When I enter 1, it always displays that the TV has been turned off, and never displays it being turned on. But when I entered 3 the volume is still being increased successfully while it should fail and display a text describing that the volume increase should have failed.
And here's the code:
class Television:
"""Get yourself some televisions"""
def __init__(self, channel, volume, is_on):
self.channel = channel
self.volume = volume
self.is_on = is_on
is_on = False
def __str__(self):
string = "\nStatus: "
string += "\nChannel " + self.channel
string += "\nVolume" + self.volume
if is_on == True:
string += "\n" + self.is_on
return string
def toggle_power(self):
if self.is_on == True:
self.is_on += False
print("The TV has been turned off.")
if self.is_on == False:
self.is_on += True
print("The TV has been turned on.")
def get_channel(self):
print("You're currently in channel", self.channel)
return self.channel
def set_channel(self):
if self.is_on == True:
self.channel = float(input("Enter the channel number you want to switch to."))
if current < 500 and current > 0:
print("You have been switched to channel", self.channel)
else:
print("Channel not found.")
elif self.is_on == False:
print("The TV has been turned off.")
def raise_volume(self):
if self.is_on == True:
if self.volume < 10:
self.volume += 1
print("The volume has been successfully increased. Your current volume is", self.volume)
else:
print("The attempt to raise the volume level was unsuccessful.")
elif self.is_on == False:
print("The TV has been turned off.")
def lower_volume(self):
if self.is_on == True:
if self.volume > -1:
self.volume +- 1
print("The volume has been successfully lowered. Your current volume is", self.volume)
else:
print("The attempt to lower the volume level was unsuccessful.")
elif self.is_on == False:
print("The TV has been turned off.")
def main():
choice = 1
tv1 = Television(1, 5, True)
while choice != 0:
choice = int(input("""0 - Exit
1 - Toggle Power
2 - Change Channel
3 - Raise Volume
4 - Lower Volume
"""))
if choice == 1:
tv1.toggle_power()
if choice == 2:
tv1.get_channel
tv1.set_channel
if choice == 3:
tv1.raise_volume()
if choice == 4:
tv1.lower_volume()
main()
These issues are just severe...Please tell me if there are ways to fix them!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论