Jump to content

Problem with my python program

Go to solution Solved by fizzlesticks,

When referring to member variables inside of a class you have to use self.

So when printing the lights you'll need to change it to self.redlight, self.yellowlight, self.greenlight

Hi guys,

so I am very new to object oriented programming, or any programming in that matter and I have not figured it out quite yet, can you tell me whats wrong here?

class TrafficLight:

    def __init__(self, redlight, yellowlight, greenlight):
        self.redlight = redlight
        self.yellowlight = yellowlight
        self.greenlight = greenlight

    def show_status(self):
        print("Redlight: ", str(redlight))
        print("Yellowlight: ", str(yellowlight))
        print("Greenlight: ", str(greenlight))

tlight_0 = TrafficLight(0, 0, 1)

tlight_0.show_status()

So as you can see I want the traffic light to print its status regarding each light, but as it seems the function "show_status" cannot see the values I set in __init__, how do I make it so it can see them?

	line 9, in show_status
    print("Redlight: ", str(redlight))
NameError: name 'redlight' is not defined

I am sure its pretty easy, sorry if its cringing you out!

 

EDIT: Python 3.5

Link to comment
https://linustechtips.com/topic/636371-problem-with-my-python-program/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×