I have stated to make a program called virtual Tv, I have been having troubles with variabels and I need the variable: state to be able to be change and accessed outside the class and inside, but I think that state is a local variable at the moment.

import random
state=0
class TV(state):
    tv=None
    def __init__(tv):
        tvslct=("40 inch LCD","70 inch 3D","15 inch old school")
        tv=random.choice(tvslct)
        print("You have been given a",tv,"Tv.")
        print("The Tv has been setup")
    def turnon(state):
        if state==1:
            print("The Tv is already on.")
        else:
            state=1
            print("The Tv has turned on.")
    def turnoff(state):
        if state==0:
            print("The Tv is already off.")
        else:
            state=0
            print("The Tv has turned off.")
    def changechannel(state):
        if state==1:
            print("It has changed.")
        else:
            print("The action could not be completed as the Tv is turned off.")
    def findchannel(state):
        if state==1:
            prevchannels=channels
            channels=randint(0,15)
            while channels < prevchannels:
                randint(0,15)
            print("The total Tv channels has been updated.")
            print("The Tv now has",channels,"channels.")
        else:
            print("The action could not be completed as the Tv is turned off.")
        
TV()
TV.turnon(state)
print(state)
TV.turnoff(state)
print(state)
TV.turnon(state)
print(state)

Could you please edit the code to change it and I will be able to learn from the differences, thanks :)

Something like this :

import random

class TV(object):
    def __init__(self): # self refers to the object itself
                        # Don't mix up an object instance and an object class.
                        # Class is the definition of a type of object (for example TVs all have a brand and channels)
                        # instance is one particular object (the tv with its serial number)
        tvslct=("40 inch LCD","70 inch 3D","15 inch old school")
        self.tv = random.choice(tvslct)  # self.something refers to the variable something affected to the object instance itself
        self.state = 0
        self.channel = 1
        print("You have been given a",self.tv,"Tv.")
        print("The Tv has been setup")

    def turnon(self):
        if self.state==1: # The state of the object instance
            print("The Tv is already on.")
        else:
            self.state=1
            print("The Tv has turned on.")
    def turnoff(self):
        if self.state==0:
            print("The Tv is already off.")
        else:
            self.state=0
            print("The Tv has turned off.")

    def changechannel(self, channel):
        if self.state==1:
            self.channel = channel # I affect the channel to the corresponding object instance variable
            print("It has changed.")
        else:
            print("The action could not be completed as the Tv is turned off.")

    def findchannel(self):
        # I don't undestand what you expect this to do so I can't correct...
        if state==1:
            prevchannels=channels
            channels=randint(0,15)
            while channels < prevchannels:
                randint(0,15)
            print("The total Tv channels has been updated.")
            print("The Tv now has",channels,"channels.")
        else:
            print("The action could not be completed as the Tv is turned off.")
        
my_tv = TV()               # I create a new tv and store it in a variable
                           # This is where the definitions stored in my class are instanciated for this particular object
my_second_tv = TV()        # If I want a second one, i store it in a second variable (object are some kind of super rich types
my_tv.turnon()             # I turn on one of my tv but not the other
print(my_tv.state)         # see how state (mentionned as self.state in the object namespace) is called outside
print(my_second_tv.state)  # by mentioning one particular object

my_tv.changechannel(5)     # I can also change the channel of one tv
print(my_tv.channel)

my_tv.turnoff()
print(my_tv.state)

my_tv.turnon()
print(my_tv.state)

Edited 5 Years Ago by jice: add details

Thanks alot for your help now the gaps within my knowledge about classes and objects is filled! :)

Especially line 41 does not make any sense.

It is a virtual tv and I wanted to make it have as many options as possible to extend my learning about classes and objects.

I was refering to jice comments on last method. What happens when channels is less than prevchannels. Line 41 in your code in jice's post does nothing and it is only action in the loop.

Line 41 = Line 32 in your code. It is an infinite loop;

while channels < prevchannels:
    randint(0,15)

Also the variable "channels" has not been defined for the findchannel scope.

def findchannel(state):
    if state==1:
        prevchannels=channels <--- channels not yet defined

I would suggest that you add a test function to test each function as there are other errors. You will get scope errors because variables have not been declared in many functions as well as from those function calls. Test the code and get it to the point where it will at least run and then post back with the test results that are incorrect, or the __complete__ error message if there is an error.

Edited 5 Years Ago by woooee: n/a

I like to code a simple switch using True or False. When you need to flip the switch then changing state is as easy as:

#Switch in the 'off' state
state = False
#Flip the switch
state = not state
#Switch is now in the 'on' state
#Flip again
state = not state
#Switch is back 'off'
Comments
agree
This article has been dead for over six months. Start a new discussion instead.