-1

I am making a system that reads a file, moves it to a new file, and moves it back with a new variable at the end of each line. It is saying that linesRead is referenced before assignment

import random
handVar = 0
handList = []
linesRead = 'test'
def dealCards(numPlayers, cardList):
    random.shuffle(cardList)
    for i in range(0, numPlayers):
        poppedCard1=cardList.pop()
        poppedCard2=cardList.pop()

        hand = (poppedCard1 + ', ' + poppedCard2)
        handList.append(hand)

        cardFile = open('players.txt', 'r')
        playerLines= cardFile.readlines()
        cardFile.close()
        for i in playerLines:
            for f in i:
                linesRead = linesRead + f
        print(linesRead)
        tempFile = open('tempFile.txt', 'w')
        tempFile.write(playerLines)
        tempFile.close
        tempFile = open('tempFile.txt', 'r')
        playerFile = open('players.txt', 'a')
         for i in tempFile:
            newLine= (i + ', ' + handList[handVar] + ', ' + handList[handVar+1])
            playerFile.write(newLine)
            handVar = handVar + 2
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Does this answer your question? [UnboundLocalError on local variable when reassigned after first use](https://stackoverflow.com/questions/370357/unboundlocalerror-on-local-variable-when-reassigned-after-first-use) – wjandrea May 06 '21 at 23:13
  • Or [Python 3: UnboundLocalError: local variable referenced before assignment](https://stackoverflow.com/q/10851906/4518341) – wjandrea May 06 '21 at 23:14

1 Answers1

0

The reason for the error scopes of the variables.

You are defining the variable linesRead in the global scope (outside of the function). It is available inside the function as long as you do not try to reassign the variable. When you reassign, Python treats is as a local scope variable (the scope of the function), and since you are using it in the reassignment, it is not able to find the assignment of that var in local scope.


Let's look at the following examples:
num = 42


def foo():
    print(num)
    
    
foo()

Output:

42

In this case num is a variable defined in the Global scope (aka. module scope). It is defined at the top level of the module and is available in the local scopes of functions.

But the things are different in the following example:

num = 42


def foo():
    print(num)
    num = 12
    
    
foo()

Output:

UnboundLocalError: local variable 'num' referenced before assignment

What happens in this case is Python making the num a variable of the local scope of the foo function. That is it has nothing to do with the num of the global scope. Since I'm trying to print it before I assign it a value, it gives me an error.

Things are different when I move the assignment above of the print function call:

num = 42


def foo():
    num = 12
    print(num)
    
    
foo()

Output:

12

Same happens in this case, but since num is already assigned in the local scope by the time I'm calling the print, it won't error.


Solution

One of the solutions is to include the var as a global in the function. Something like this

def dealCards(numPlayers, cardList):
    global linesRead

it will make the var from global variable available at the local scope.

But in this case if you modify the var in the function, the one at the module level will be changed as well

num = 42


def foo():
    global num
    print(num)
    num = 12
    

foo()
print(num)

Output

42
12

Here is a fine, detailed read on Python scopes: https://realpython.com/python-scope-legb-rule/
Henry Harutyunyan
  • 2,355
  • 1
  • 16
  • 22