[Tutor] Television simulation
Dave Angel
d at davea.name
Fri Dec 23 22:57:19 CET 2011
On 12/23/2011 04:43 PM, Dave Angel wrote:
> On 12/23/2011 04:21 PM, myles broomes wrote:
>> Im trying to create a 'Television simulation' program. Heres the code
>> ive written for it:
>>
>> #television simulation
>> #a program that simulates a television
>> #the user can turn the television on or off, change the volume or
>> change the channel
>>
>> #create the television class
>> class Television(object):
>> """A virtual television simulation"""
>> def __init__(self):
>> print("The television is off.")
>>
>> def power_button(self, power = "off"):
>> if power == "off":
>> power = "on"
> The above line does nothing useful, as the value is thrown out when
> the method returns. Same is true in several other places below.
>> print("The power is now on.")
>> else:
>> power = "off"
>> print("The power is now off.")
>>
>> def volume_button(self, volume = 0):
>> up_or_down = input("Do you want to increase or
>> decrease the volume? (up/down): ")
>> if up_or_down == "up":
>> amount = int(input("By how much? (Enter a
>> number): "))
>> volume += amount
>> if volume> 10:
>> volume = 10
>> print("The volume is now",volume)
>> elif up_or_down == "down":
>> amount = int(input("By how much? (Enter a
>> number): "))
>> volume += amount
>> if volume< 0:
>> volume = 0
>> print("The volume is now",volume)
>> else:
>> print("That is not a valid choice.")
>>
>> def channel_button(self, channel = 1):
>> new_channel = int(input("What channel do you want to
>> watch? (Enter a number between 1 and 10.): "))
>> if new_channel< 1 or new_channel> 10:
>> print("That is not a valid channel!")
>> else:
>> channel = new_channel
>> print("The channel is now",channel)
>>
>> #create the main part of the program, the television simulation
>> def main():
>> tv = Television()
>>
>> choice = None
>> while choice != "0":
>> print \
>> ("""
>> Television simulation
>>
>> 0 - Quit
>> 1 - Turn the television on or off
>> 2 - Change the volume
>> 3 - Change the channel
>> """)
>>
>> choice = input("Choice: ")
>> print()
>>
>> #exit
>> if choice == "0":
>> print("Good-bye.")
>>
>> #turn the television on or off
>> elif choice == "1":
>> tv.power_button()
>>
>> #increase or decrease the volume
>> elif choice == "2":
>> tv.volume_button()
>>
>> #change the channel
>> elif choice == "3":
>> tv.channel_button()
>>
>> else:
>> print("\nInvalid choice!")
>>
>> main()
>> ("\n\nPress the enter key to exit.")
>>
>>
>> It works fine but the problem im having is that when volume, channel
>> or power are changed inside of their methods, their values dont
>> change in the program if that makes sense. So i was just wondering if
>> there was a way around this.
>>
> Normally when such values are changed in the method, you want a
> corresponding attribute of the instance to 'remember' the value. In
> your particular program you have one instance, called tv. Each time
> you call a method on that instance, such as tv.power_button(), you are
> implicitly passing that instance to the method, as the value 'self'.
> That's what you're not writing to correctly.
>
> Inside a method, you usually refer to such instance attributes as
> self.attribname. So let's try just one of them, the power button.
>
> def power_button(self):
> if self.power == "off":
> self.power = "on"
>
> else:
> self.power = 'off'
> print "Power is now ", self.power
>
> Notice I got rid of the unused parameter, since it was never referenced.
>
> One other thing we must do here: In the __init__() method, you need
> to initialize the state of the Television instance. You can't just
> print a statement saying it's initialized, you have to create each of
> the attributes comprising its initial state. In our case, we'd add a
> line
> self.power = "off"
>
>
> I'll leave the other two attributes to you. There are other things I
> could critique, but I want to give you the minimum push to make
> something that could run.
>
Bah - It lined up when I typed it, but I pasted some of the original,
and typed the rest. Probably your email was html, when it should have
been text. Let's try again, indenting 4 spaces instead, a much more
reasonable number than 8.
def power_button(self):
if self.power == "off":
self.power = "on"
else:
self.power = "off"
print "Power is now ", self.power
--
DaveA
More information about the Tutor
mailing list