Search code examples
pythonpython-3.xpython-turtle

Variable bullPostion is not being seen


I have 2 variables specifically called "cowPosition" and "bullPosition" however its not being seen inside the function

Here is a snippet of my code

cowPosition = 1
bullPosition = 1

def gameplay():
    setposition(True) # Sets the starting position for both bull and cow
    while cowPosition or bullPosition >= 25:
        bullPosition += 1
        print(bullPosition)

What I want it too do is that it will loop 25 times until bullPosition = 25


Solution

  • The quick solution is to declare local variables rather than global ones:

    def gameplay():
        cowPosition = 1
        bullPosition = 1
        while cowPosition or bullPosition >= 25:
            bullPosition += 1
            print(bullPosition)
    

    To understand how this works, I suggest you read about the difference between local and global scopes.