如何制作一个合适的物体?

2024-06-03 15:02:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我要制造一台假电视。 挑战在于
编写一个模拟电视的程序,将其创建为 对象。用户应该能够输入频道编号和 提高或降低音量。确保通道号 音量保持在有效范围内。 我一直收到一个错误我现在的代码是

 class Television(object):
    """fake tv"""
    def tv(volume = 0,chanel = 0):
        tv.volume = vol
        tv.chanel = chan

    def volu(volume):
        if vol > 100:
            print('That volumes too high')
        else:
            return vol


    def channel(chanel):
        if chanel > 64:
            print('THat channel doesnt exist')

def main():    
    choice = None  
    while choice != "0":
        print \
        ("""
       YO TV

        0 - Turn off tv
        1 - Tv status
        2 - Change channel
        3 - Change volume
        """)

        choice = input("Choice: ")
        print()

        # exit
        if choice == "0":
            print("Good-bye.")

        # tv status
        elif choice == "1":
            print('The volume is ',tv.volume, 'The channel is ', tv.chanel) 

        # channel control
        elif choice == "2":
            chanchange= int(input('What channel do you want to watch'))
            tv.channel()
            print(chan,' is the channel')


        # volume control
        elif choice == "3":
            volchange= int(input('What would you like the volume to be'))
            tv.channel
            print(vol,' is the volume level')

        # some unknown choice
        else:
            print("\nSorry, but", choice, "isn't a valid choice.")

    main()
    ("\n\nPress the enter key to exit.")

Tags: thetoinputifisdefchanneltv
1条回答
网友
1楼 · 发布于 2024-06-03 15:02:10

下面是一个示例,其中的注释指出了一些问题:

class Television: #all python3 classes inherit from object
        def __init__(self, volume = 0, channel = 0):
                self.volume = volume
                self.channel = channel

        def setVolume(self, volume): #reference to instance as first arg
                if not 0 <= volume <= 100:
                        print('Volume out of bounds.')
                        return #return without setting member
                self.volume = volume #setting member

        def setChannel(self, channel):
                if not 0 <= channel<= 100:
                        print('Channel out of bounds.')
                        return
                self.channel = channel

        def __str__(self): #for printing it
                return 'Volume is {} and channel is {}.'.format(self.volume, self.channel)

tv = Television() #calling init

while True:
        choice = input('''YO TV

0 - Turn off TV
1 - TV status
2 - Change channel
3 - Change volume
''')
        if choice == '0': break
        if choice == '1':
                print(tv) #calling __str__
                continue
        if choice == '2':
                tv.setChannel(int(input('New channel: ')))
                continue
        if choice == '3':
                tv.setVolume(int(input('New volume: ')))
                continue
        print('Unkown command.')

或使用属性:

^{pr2}$

相关问题 更多 >