0

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 by monkl: indents wouldn't show, no lines

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by woooee
0

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 by woooee: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.