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()

Edited 4 Years Ago by monkl: indents wouldn't show, no lines

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)

Edited 4 Years Ago by woooee: n/a

This article has been dead for over six months. Start a new discussion instead.