Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Python Help - Simple (X , Y) coord display  (Read 3743 times)

0 Members and 1 Guest are viewing this topic.

monkl

    Topic Starter


    Starter

    • Experience: Beginner
    • OS: Unknown
    Python Help - Simple (X , Y) coord display
    « on: January 17, 2012, 12:44:19 PM »
    I am trying to create a program that returns an (X , Y) coord that increases or decreases based on input of either X, -X, Y, or -Y. However, when I run it 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),©
       grid()
    grid()

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: Python Help - Simple (X , Y) coord display
    « Reply #1 on: January 17, 2012, 01:00:02 PM »
    Code: [Select]
    #!/usr/bin/python

    def grid():
       x = 0
       y = 0
       while(True):
           print("Which way would you like to move? X, -X, Y, or -Y? (Q to quit):")
           a = raw_input()
           if a == "X":
              x = x + 1
           elif a == "-X":
              x = x - 1
           elif a == "Y":
              y = y + 1
           elif a == "-Y":
              y = y - 1
           elif a == "Q":
               return
           print (z),(c)
    grid()

    There were some logic errors in the original code as well.

    First, you never actually change the contents of z or c. you calculate the new coordinate but you never store it into the value.
    Second, the structure was... ill-thought out. The way it was worked was as a recursive function call; each prompt was in fact a recursive call. The problem being that recursion typically requires an end case that is being worked towards; however, it was being used as a poor substitute for a loop. (and WILL crash if it is run too long from a stack overflow) My version uses a while loop instead; as a result, since the variables are only accessed in the local routine (which doesn't exit until the user enters "q") making the variables global is no longer necessary.

    Also, the original code was littered with non-ascii characters; "smart quotes", a em-dash (rather than a minus sign) and a few others. Not sure how they got into the code. using the "code" bbcode tag can help prevent this, though the forum doesn't add smart quotes or em-dash's so I'm not sure where those came from.
     
    I was trying to dereference Null Pointers before it was cool.

    monkl

      Topic Starter


      Starter

      • Experience: Beginner
      • OS: Unknown
      Re: Python Help - Simple (X , Y) coord display
      « Reply #2 on: January 17, 2012, 01:06:03 PM »
      Thank you so much! I am a begginner at Python, the only programming language I have any experience with is BASH, and learning without a tutor has been rather difficult Thanks for the help!