Advertisement
Steuer

Создайте программу, имитирующую телевизор как объект...

Apr 25th, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  1. # TV Controller
  2. # Select a channel and change the volume
  3.  
  4. class TV(object):
  5.  
  6.     def __init__(self, channel = 0, volume = 5):
  7.         self.channel = channel
  8.         self.volume = volume
  9.  
  10.     def change_channel(self):
  11.         self.channel = int(input("Choose a channel between 1 to 3: "))
  12.         if 0 < self.channel <= 3:
  13.             print("\nOk, channel", self.channel)
  14.         else:
  15.             print("\nSorry, but", self.channel, "isn't a valid choice.")
  16.  
  17.     def decrease_volume(self, change = 1):
  18.         self.volume -= change
  19.         if self.volume < 0:
  20.             self.volume = 0
  21.         print("Now volume is", self.volume)
  22.  
  23.     def increase_volume(self, change = 1):
  24.         self.volume += change
  25.         if self.volume > 10:
  26.             self.volume = 10
  27.         print("Now volume is", self.volume)
  28.  
  29. def main():
  30.     tv = TV()
  31.  
  32.     choice = None
  33.     while choice != "0":
  34.         print \
  35.         ("""
  36.         TV Controller
  37.  
  38.         0 - Quit
  39.         1 - Change a Channel
  40.         2 - Decrease a Volume
  41.         3 - Increase a Volume
  42.         """)
  43.  
  44.         choice = input("Choise: ")
  45.         print()
  46.  
  47.         # exit
  48.         if choice == "0":
  49.             print("Good-bye.")
  50.  
  51.         # change a channel
  52.         elif choice == "1":
  53.             tv.change_channel()
  54.  
  55.         # decrease a volume
  56.         elif choice == "2":
  57.             tv.decrease_volume()
  58.  
  59.         # increase a volume
  60.         elif choice == "3":
  61.             tv.increase_volume()
  62.  
  63.         # some unknown choice
  64.         else:
  65.             print("\nSorry, but", choice, "isn't a valid choice.")
  66.  
  67. main()
  68. input("\n\nPress the enter key to exit.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement