[Solved] Variable not being recognized?

The variable is defined on line 44 and used on lines 76 and 79.I'd really appreciate if someone could help me out. I'm thinking it's probably some stupid spacing issue or something, but the problem totally eludes me.

Post a Reply

Replies

You defined it as global variable(its out of any scope). You didnt specifyed it in gameLoop as global so u cant access it.You creating another one locally this time where you assign it a value 5 or -5. So you have 2 variables now, diferent from each other.Just put under your gameLoop, global car_x_change and then you will have access and change it(so yoy will have one global variable).

PS: Thats why PyCharm says you didnt used the local variable, it sees it as a new variable(the local one).Sorry for my bad english

EDIT: my post refers to the pastebin code, in your 2nd post you changed it. Well this time you created a local variable so you can only use it inside this function gameLoop. Again, if you assigned it outside of any function (globally), then inside a function in order to change it's value you have to specify it as a global variable so function can understand that this variable is already defined somewhere else.

Seems fine to me, what's the issue? try using print(car_change_x) to make sure your key presses are being logged. According to this code, all you are doing is changing the value for car_x_change from -5 to 5 depending on whether the left arrow or right arrow keys are pressed.