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

时间:2013-11-20 02:18:06

标签: python object python-3.x

我必须制作假电视的OOP。 挑战是 编写一个模拟电视的程序,将其创建为 宾语。用户应该能够输入频道号码 提高或降低音量。确保频道号 和音量水平保持在有效范围内。 我继续收到错误,我现在的代码是

 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.")

1 个答案:

答案 0 :(得分:0)

这里有一个注释指向某些问题的示例:

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.')

或使用属性:

class Television:
        def __init__(self, volume = 0, channel = 0):
                self.__volume = volume
                self.__channel = channel

        @property
        def volume(self): return self.__volume

        @volume.setter
        def volume(self, volume):
                if not 0 <= volume <= 100:
                        print('Volume out of bounds.')
                        return
                self.__volume = volume

        @property
        def channel(self): return self.channel

        @channel.setter
        def channel(self, channel):
                if channel < 0 or channel > 64:
                        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.channel = int(input('New channel: ')) #note here
                continue
        if choice == '3':
                tv.volume = int(input('New volume: ')) #note here
                continue
        print('Unkown command.')