So I'm working on a game, and here is my current code, which works fine:

print("Type directions() to see commands" )

def directions() :

    print("Type North, East, South or West to move" )
    move = input("North, East, South or West? ")
    if move == "North":
    north()
    elif move == "East":
    east()
    elif move == "South":
    south()
    elif move == "West":
    west()


def north():
    print("You moved North")
    x=0
    x=(x+1) 
    print("You are at co-ordinates", x, "x:0 y" )

However, I am aware that later there will be a problem as x would be reset for each direction. When putting x=0 at the top of the code and removing it from the def north() part, when running the code, Python gives me the error:

'UnboundLocalError: local variable 'x' referenced before assignment'

How can I code this so that 'x' is 0 to begin with, but can be altered by def functions?

有帮助吗?

解决方案

You can do one of 2 things: either use a global variable (which should be mostly avoided), or use return values. The global method:

x=0
.
.
def directions():
    .
    .
def north():
    global x
    x=x+1
    print("moved north...")

or, the return value:

def directions():
    current_location = 0
    .
    .
    if move == "North":
         current_location = North(current_location)
    .
    .
def North(location):
    print("moved north...")
    return location+1

Judging by your question and the existence of y, you can do the following:

def directions():
.
.
    if move == "North":
        (x,y) = North(x,y)
.
.
def North(x,y):
    return (x+1,y)
def East(x,y):
    return (x,y+1)
def South(x,y):
    return (x-1,y)

其他提示

Some tips (not exhaustive, but enough for a start):

First, use a global dictionary for your directions:

def north(x):
    return x+1

directions = {'North': north, 'East': east, 'South': south, 'West': west}
def move(x) :
    global directions
    print("Type North, East, South or West to move")
    move_choice = input("North, East, South or West? ")
    # modify lambda to your logic to handle incorrect inputs
    x = directions.get(move, lambda y:y)(x)
    repr(x)
    return x

and if you want functional approach (as opposed to OOP), your functions has to accept some arguments:

def north(x):
    return x+1

Don't mix your logic and representation, this way you'll have to copy alot of code:

def repr(x):
    print("You are at co-ordinates {} x:0 y".format(x))

and combine them in main routine:

def main():
    x = 0
    while True: # modify to be able to terminate this loop
        x = move(x)

Along the lines of what Cygwinnian suggested, making x a global variable will fix the problem. That means the same x variable is accessible from all functions which declare global xbefore they use x.

To fix your error:

x = 0

print("Type directions() to see commands" )

def directions() :
    print("Type North, East, South or West to move" )
    move = input("North, East, South or West? ")
    if move == "North":
    north()
    elif move == "East":
    east()
    elif move == "South":
    south()
    elif move == "West":
    west()


def north():
    global x
    print("You moved North")
    x=(x+1) 
    print("You are at co-ordinates", x, "x:0 y" )
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top