Undefined Variable Errors in Python Code

I am currently developing a console/text based game in Python, and I have been experiencing an undefined variable error in my code. However, I have checked my code and found that the variable was defined and should be valid. I am wondering why this is happening.
##Link to Repl##
##Code Snippet##
Unfortunately, the piece of code in question is too large to copy/paste into the forum, so I have provided the link to the repl instead. If you run the repl, it should show the error and where it is.

Before I further examin your code, is the free variable AIMOVES the problem you are experiencing?
Since there are many input prompts I am not sure if that’s the thing you have experienced, as it may be different due to different inputs

Also omg the code is huge, I might take a long time before being able to assist you

@TaokyleYT , yeah that’s correct. AIMOVES is the problem I’m having. Sorry for not being clearer on that topic.

So, all definitions to AIMOVES before call of function aiturn(which includes the error code in line 1559) is under if/elif conditions about aichoice , therefore let’s go back to the definition of aichoice before the if/elif conditions.

aichoice was defined as integer 0 in line 509, and the following lines are if/elif conditions about player1choice, player2choice and aichoice.

In the if/elif conditions of aichoice, the first if condition is checking if aichoice is integer 1, and the following elif conditions check if the same thing is equal to some numbers after 1, no else conditions are included in the if/elif condition.

Therefore, that 0 defined doesn’t match any condition to assign the AIMOVES, which the reference in line 1559 is confused as it doesn’t find any definition executed.

Solution: change the aichoice = 0 in line 509 to any integer from 1 to 11.

additional suggestions (obviously optional): you can use match and case keyword to replace those big if/elif statements to increase readability and performance of those parts of code, syntax:

match variable:
    case 1: #if
    case 2: #elif
    case 3: #elif
    case _: #else

which is equal to

if variable == 1:
elif variable == 2:
elif variable == 3:

It can be extended base on the syntax

1 Like

some other suggestions I will add:

Instead of using match case or if elif for many similar values, use a dictionary. (Also, don’t worry about performance here.)

There’s also a massive amount of code duplication in the codebase that should be addressed.

The DRY principle, Don’t Repeat Yourself, is important.
There are many ways to greatly reduce the duplicated code, using dictionaries and functions.

1 Like

Thanks for the solution, but I don’t think the aichoice variable would be set to 0 if the gametype variable was also set to 0. In the code, it says:

aichoice = random.randint(1,9)

Therefore, the aichoice value should not be 0 in any case unless somehow the code wasn’t executed.

He is saying that on line 509, you set aichoice to 0 before you check what number it is. So you are overwriting the random value that you generated.

Oh, sorry for the confusion. It turns out that that was a self error with the indentation level, since I was trying to debug a part of the program where the gametype variable was set to 1 (in which the aichoice variable would not be set)

Just wondering, where is this line of code
(1686 lines of code really hurts my eye lol I can’t even find it)

Also, please consider refactoring your codebase to use more functions because the code duplication is getting out of hand. You can also separate your code into more files to more easily navigate and organize everything.

1 Like

Though it is hard to tell in all of this duplicated code, it is line 365. Unfortunately, you cannot ctrl-f it in the cover page. (I did make a feature request about this.)

Apologies for the duplicated code. I’ll try to fix it at some point.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.