I am trying to get a program to return a coord that increases or decreases in either X or Y given an input of either X, -X, Y, or -Y. It starts at (0 , 0). When I run it however, it always returns (0 , 0). What am I doing wrong? Here is my code.


#!/usr/bin/python
#Simple Grid Display - B. Owens
def grid():
#Z is the X coord, C is the Y coord
global z
global c
z = 0
c = 0
print (“Which way would you like to move? X, -X, Y, or –Y?”)
a = raw_input()
if a == “X”:
z + 1
if a == “-X”:
z – 1
if a == “Y”:
c + 1
if a == “-Y”:
c – 1
print (z),(c)
grid()
grid()

I would suggest that you start with a tutorial on passing and returning parameters in functions like this one, and also read up on changing variables, see "Arithmetic Operators" here, as this question is covered in every tutorial and too simple to be asking someone else about. Your code, modified so it runs

def grid(c, z):  #Z is the X coord, C is the Y coord
    print ("Which way would you like to move? X, -X, Y, or -Y?")
    a = raw_input()
    if a == "X":
        z += 1
    elif a == "-X":
        z -= 1
    elif a == "Y":
        c += 1
    elif a == "-Y":
        c -= 1

    return c, z

c = 2
z = 3
print(c, z)
for ctr in range(3):
    c, z = grid(c, z)
    print(c, z)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.