Personal tools

Number of visitors
since 27 March 2006
 
Document Actions

Simulating a TV set with Python

Click here to start saving with ING DIRECT!

Digg!

I started learning pythong a long time ago but lack of driver in learning has meant that my learning curve has been very flat so far. Sometimes I used to work on learning quite rigorously and sometimes I would not look at Python for a long long time. I am in the middle of something like a hiatus now. When I first started learning I got stuck on a programming exercise to create a python program that simulates a TV set from Michael Dawson's book Python programming for the Absolute Beginner. No matter how hard I tried I was not able to write the correct functions to make the program run and I had almost given up. Years later when I started again, I started afresh with Magnus L Hetland's Beginning Python: From Novice to Professional. It gave a much better in-depth look on functions and how to build it and I was finally able to create the TV simulation program.

I am posting the code here for anyone to view and comment. If you feel that this could be implemented in a better way, then please let me know. As you can see from the dates, it took me quite a while to spend time and complete the program.

# Python Programme Number 66
# Simulate a Television set
# Programmer: Mukul Dharwadkar
# Date: 8 September 2006
# Final Date: 9 June 2009

class Television(object):
    """A virtual Telly"""
    def __init__(self, channel=1, volume=10):
        self.channel = channel
        self.volume = volume
        """ Empty module"""
    
    def chg_chn(self):
        self.channel = input("Please select the channel you want to watch: ")
        while self.channel > 100 or self.channel < 0:
            print "Please enter a valid number."
            self.channel = input("Please select the channel you want to watch: ")
        return self.channel

    def chg_vol(self):
        self.volume = input("Please select the volume level: ")
        while self.volume > 60 or self.volume < 0:
            print "Please enter a valid number."
            self.volume = input("Please select the volume level: ")
        return self.volume

    #volume = property(chg_vol)

    def chn(self, channel=1):
        print "Currently you are watching channel no: ", self.channel
        #self.channel()
    def vol(self):
        volume = self.volume
        print "The volume level is: ", self.volume

def main():
    """This is the main module and will call the other modules"""
    tv = Television()

    choice = None
    while choice != "0":
        print \
              """
                Television Menu

                0 - Switch Off
                1 - Select Channel to watch
                2 - Select volume level
                3 - Display channel
                4 - Display volume level
                """

        choice = raw_input("Make your selection: ")
        print

        #Switch Off
        if choice == "0":
            print "Good Bye"

        # Change the channel
        elif choice == "1":
            tv.chg_chn()

        elif choice == "2":
            tv.chg_vol()

        elif choice == "3":
            tv.chn()

        elif choice == "4":
            tv.vol()

        else:
            print "\nSorry, but", choice, "isn't a valid selection."

main()
#raw_input("\n\nPress enter to exit")
The URL to Trackback this entry is:
http://www.dharwadkar.com/weblog/python_tv_code/tbping
Add comment

You can add a comment by filling out the form below. Plain text formatting.

(Required)
(Required)

Cloud VPS Hosting

Acronis True Image 2012 Special offer 125x125
iTunes, App Store, iBookstore, and Mac App Store

 
    This site is: