Coordinate Grid Points

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric.Gabrielson@gmail.com

    #1

    Coordinate Grid Points

    Hello,
    I am very knew to python and am attempting to write a program
    in python that a friend of mine is having to write in java. I am doing
    this for fun and would like some help as to how i can generate random
    coordinate points (x,y) and compare them with user inputted coordinate
    points. For example how will I be able to access the separate values,
    the x from the x,y position. I think I understand everything except
    1)the random coordinate points 2) the getting the users inputted x and
    y values in one line ("Please guess a coordinate point from (1,1) to
    (20,20): ") as opposed to ("Please enter an x value:" and "Please
    enter a y value") and finally 3) acessing the x value from the x,y
    coordinate function. the assignment description is located here http://

    homework.html if you would like to see a more in depth discription of
    the game.

    Many Thanks,
    Eric

  • James Stroud

    #2
    Re: Coordinate Grid Points

    Eric.Gabrielson @gmail.com wrote:
    Hello,
    I am very knew to python and am attempting to write a program
    in python that a friend of mine is having to write in java. I am doing
    this for fun and would like some help as to how i can generate random
    coordinate points (x,y) and compare them with user inputted coordinate
    points. For example how will I be able to access the separate values,
    the x from the x,y position. I think I understand everything except
    1)the random coordinate points 2) the getting the users inputted x and
    y values in one line ("Please guess a coordinate point from (1,1) to
    (20,20): ") as opposed to ("Please enter an x value:" and "Please
    enter a y value") and finally 3) acessing the x value from the x,y
    coordinate function. the assignment description is located here http://

    homework.html if you would like to see a more in depth discription of
    the game.
    >
    Many Thanks,
    Eric
    >
    For 1: see the random module (e.g. random.randint)
    For 2: see the "eval" function and "raw input"
    For 2 (without cheating): see the re module. For example:

    map(int, re.compile("\(? (\d+),(\d+)\)?" ).search(inpt). groups())

    (Giving you the latter because eval will do this for you anyway.)

    Also see "raw_input" .

    James

    Comment

    • James Stroud

      #3
      Re: Coordinate Grid Points

      Eric.Gabrielson @gmail.com wrote:
      Hello,
      I am very knew to python and am attempting to write a program
      in python that a friend of mine is having to write in java. I am doing
      this for fun and would like some help as to how i can generate random
      coordinate points (x,y) and compare them with user inputted coordinate
      points. For example how will I be able to access the separate values,
      the x from the x,y position. I think I understand everything except
      1)the random coordinate points 2) the getting the users inputted x and
      y values in one line ("Please guess a coordinate point from (1,1) to
      (20,20): ") as opposed to ("Please enter an x value:" and "Please
      enter a y value") and finally 3) acessing the x value from the x,y
      coordinate function. the assignment description is located here http://

      homework.html if you would like to see a more in depth discription of
      the game.
      >
      Many Thanks,
      Eric
      >
      For the game described, the input is of the form "20 20", so (1) eval
      won't work and (2) regex is not needed. So you should use a healthy
      combo of map(), inpt.split(), int(), and inpt.strip() (assuming inpt is
      the string input). But not necessarily in the order listed.

      Comment

      • Eric.Gabrielson@gmail.com

        #4
        Re: Coordinate Grid Points

        On Feb 5, 3:29 pm, James Stroud <jstr...@mbi.uc la.eduwrote:
        Eric.Gabriel... @gmail.com wrote:
        Hello,
        I am very knew to python and am attempting to write a program
        in python that a friend of mine is having to write in java. I am doing
        this for fun and would like some help as to how i can generate random
        coordinate points (x,y) and compare them with user inputted coordinate
        points. For example how will I be able to access the separate values,
        the x from the x,y position. I think I understand everything except
        1)the random coordinate points 2) the getting the users inputted x and
        y values in one line ("Please guess a coordinate point from (1,1) to
        (20,20): ") as opposed to ("Please enter an x value:" and "Please
        enter a y value") and finally 3) acessing the x value from the x,y
        coordinate function. the assignment description is located here http://

        homework.html if you would like to see a more in depth discription of
        the game.
        >
        Many Thanks,
        Eric
        >
        For 1: see the random module (e.g. random.randint)
        For 2: see the "eval" function and "raw input"
        For 2 (without cheating): see the re module. For example:
        *************** *************** *************** *************** *************** **
        ***map(int, re.compile("\(? (\d+),(\d+)\)?" ).search(inpt). groups())****
        *************** *************** *************** *************** *************** **
        (Giving you the latter because eval will do this for you anyway.)
        >
        Also see "raw_input" .
        >
        James
        Thank you very much for your response i will play around with the code
        when I have some time ( hopefully later tonight) but could you please
        breakdown what the part that I surrounded with asterisks, I think I
        recognize it but don't understand it.

        Eric

        Comment

        • James Stroud

          #5
          Re: Coordinate Grid Points

          Eric.Gabrielson @gmail.com wrote:
          On Feb 5, 3:29 pm, James Stroud <jstr...@mbi.uc la.eduwrote:
          >
          >>Eric.Gabriel. ..@gmail.com wrote:
          >>
          >>>Hello,
          >> I am very knew to python and am attempting to write a program
          >>>in python that a friend of mine is having to write in java. I am doing
          >>>this for fun and would like some help as to how i can generate random
          >>>coordinate points (x,y) and compare them with user inputted coordinate
          >>>points. For example how will I be able to access the separate values,
          >>>the x from the x,y position. I think I understand everything except
          >>>1)the random coordinate points 2) the getting the users inputted x and
          >>>y values in one line ("Please guess a coordinate point from (1,1) to
          >>>(20,20): ") as opposed to ("Please enter an x value:" and "Please
          >>>enter a y value") and finally 3) acessing the x value from the x,y
          >>>coordinate function. the assignment description is located here http://
          >>>https://www.cs.washington.edu/educat...07wi/homework/
          >>>homework.htm l if you would like to see a more in depth discription of
          >>>the game.
          >>
          >>>Many Thanks,
          >>>Eric
          >>
          >>For 1: see the random module (e.g. random.randint)
          >>For 2: see the "eval" function and "raw input"
          >>For 2 (without cheating): see the re module. For example:
          >>************* *************** *************** *************** *************** ****
          >***map(int, re.compile("\(? (\d+),(\d+)\)?" ).search(inpt). groups())****
          >>************* *************** *************** *************** *************** ****
          >>(Giving you the latter because eval will do this for you anyway.)
          >>
          >>Also see "raw_input" .
          >>
          >>James
          >
          >
          Thank you very much for your response i will play around with the code
          when I have some time ( hopefully later tonight) but could you please
          breakdown what the part that I surrounded with asterisks, I think I
          recognize it but don't understand it.
          >
          Eric
          >
          "re" is the regular expression module for python. "re.compile ()"
          compiles the regular expression into a regular expression object with
          certain attributes, one of which is "search". "search" searches a
          string, here "inpt", and this produces a "match" (actually a
          _sre.SRE_Match) object that has, as one of its attributes, a "groups()"
          method that returns matches to the grouped expressions inside the
          regular expression, i.e. expressions surrounded by un-escaped parentheses.

          Inside of the quotes is a regular expression string, (that, in general,
          should be preceded immediately by a lowercase r--but is not here because
          it doesn't matter in this case and I forgot to). map() maps a callable
          (here int) to the list of groups returned by groups(). Each group is a
          string matching r"\d+" which is an expression for one or more digits.
          Each group is converted into an integer and map returns a list of
          integers. I escaped the enclosing parentheses and put question marks
          (match zero or one) so that the enclosing parentheses would be optional.
          This makes all of the following evaluate to [20, 20]:

          "20,20"
          "(20,20"
          "20,20)"
          "(20,20)"

          Except for typos, the middle two would be quite uncommon for user input,
          but would match the expression. Note also, that I didn't allow for
          whitespace anywhere, which might be expected. Arbitrary whitespace is
          matched by r"\s*".

          James

          Comment

          • Eric.Gabrielson@gmail.com

            #6
            Re: Coordinate Grid Points

            On Feb 5, 6:33 pm, James Stroud <jstr...@mbi.uc la.eduwrote:
            Eric.Gabriel... @gmail.com wrote:
            On Feb 5, 3:29 pm, James Stroud <jstr...@mbi.uc la.eduwrote:
            >
            >Eric.Gabriel.. .@gmail.com wrote:
            >
            >>Hello,
            > I am very knew to python and am attempting to write a program
            >>in python that a friend of mine is having to write in java. I am doing
            >>this for fun and would like some help as to how i can generate random
            >>coordinate points (x,y) and compare them with user inputted coordinate
            >>points. For example how will I be able to access the separate values,
            >>the x from the x,y position. I think I understand everything except
            >>1)the random coordinate points 2) the getting the users inputted x and
            >>y values in one line ("Please guess a coordinate point from (1,1) to
            >>(20,20): ") as opposed to ("Please enter an x value:" and "Please
            >>enter a y value") and finally 3) acessing the x value from the x,y
            >>coordinate function. the assignment description is located here http://
            >>https://www.cs.washington.edu/educat...07wi/homework/
            >>homework.ht ml if you would like to see a more in depth discription of
            >>the game.
            >
            >>Many Thanks,
            >>Eric
            >
            >For 1: see the random module (e.g. random.randint)
            >For 2: see the "eval" function and "raw input"
            >For 2 (without cheating): see the re module. For example:
            >************** *************** *************** *************** *************** ***
            ***map(int, re.compile("\(? (\d+),(\d+)\)?" ).search(inpt). groups())****
            >************** *************** *************** *************** *************** ***
            >(Giving you the latter because eval will do this for you anyway.)
            >
            >Also see "raw_input" .
            >
            >James
            >
            Thank you very much for your response i will play around with the code
            when I have some time ( hopefully later tonight) but could you please
            breakdown what the part that I surrounded with asterisks, I think I
            recognize it but don't understand it.
            >
            Eric
            >
            "re" is the regular expression module for python. "re.compile ()"
            compiles the regular expression into a regular expression object with
            certain attributes, one of which is "search". "search" searches a
            string, here "inpt", and this produces a "match" (actually a
            _sre.SRE_Match) object that has, as one of its attributes, a "groups()"
            method that returns matches to the grouped expressions inside the
            regular expression, i.e. expressions surrounded by un-escaped parentheses.
            >
            Inside of the quotes is a regular expression string, (that, in general,
            should be preceded immediately by a lowercase r--but is not here because
            it doesn't matter in this case and I forgot to). map() maps a callable
            (here int) to the list of groups returned by groups(). Each group is a
            string matching r"\d+" which is an expression for one or more digits.
            Each group is converted into an integer and map returns a list of
            integers. I escaped the enclosing parentheses and put question marks
            (match zero or one) so that the enclosing parentheses would be optional.
            This makes all of the following evaluate to [20, 20]:
            >
            "20,20"
            "(20,20"
            "20,20)"
            "(20,20)"
            >
            Except for typos, the middle two would be quite uncommon for user input,
            but would match the expression. Note also, that I didn't allow for
            whitespace anywhere, which might be expected. Arbitrary whitespace is
            matched by r"\s*".
            >
            James
            Thank you for your help so far, it has been useful but i haven't quite
            gotten time to figure it out. In the mean time i was posting in a
            different forum and got given some help (to define a "point" class) I
            took that advice and am now getting an error relating to it that i
            have no idea how to fix and was wondering if you might be able to give
            me a hand.

            Anyways heres my error:
            *************** *************** *************** *************** *************** *************** *************** ***************
            ***File "C:/Documents and Settings/Eric/Desktop/Python/2d guessing
            game.py", line 11, in <module>***
            *** randp = Point(random.ra ndint(1, 20), random.randint( 1,
            20)) ***
            ***TypeError: default __new__ takes no
            parameters
            ***
            *************** *************** *************** *************** *************** *************** *************** ***************
            and heres my code:

            *************** *************** *************** *************** *************** *************** *************** ***************
            #Import Random Module
            import random

            #classify "Point"
            class Point(object):
            def _init_(self, x, y):
            self.x = x
            self.y = y

            #generate random coordinate point
            randp = Point(random.ra ndint(1, 20), random.randint( 1, 20))

            #print randp

            #get user input
            x = int(raw_input(" Enter guessed x value (1-20): "))
            y = int(raw_input(" Enter guessed y value (1-20): "))

            #compare input to randomly generated point
            attempts = 0

            while True:
            userp = Point(x, y)
            attempts += 1

            if userp.y == randp.y and userp.x == randp.x:
            print "You got it right!"
            break

            elif userp.y == randp.y:
            if userp.y randp.x:
            print "Go west"
            elif userp.x < randp.x:
            print "Go east"
            x = int(raw_input(" Enter new x guess: "))
            userp = Point(x, y)

            elif userp.y < randp.y:
            if userp.x randp.x:
            print "Go northwest"
            elif userp.x < randp.x:
            print "Go northeast"
            x = int(raw_input(" Enter new x guess: "))
            y = int(raw_input(" Enter new y guess: "))
            userp = Point(x, y)

            elif userp.x == randp.x:
            if userp.y randp.y:
            print "Go south"
            elif userp.y < randp.y:
            print "Go north"
            y = int(raw_input(" Enter new y guess: "))
            userp = Point(x, y)

            print " "
            print "Number of guesses", attempts
            *************** *************** *************** *************** *************** *************** *************** ***************

            Comment

            • greg

              #7
              Re: Coordinate Grid Points

              Eric.Gabrielson @gmail.com wrote:
              class Point(object):
              def _init_(self, x, y):
              The name of the __init__ method needs *two* underscores
              at each end, i.e.

              def __init__(self, x, y):

              --
              Greg

              Comment

              • Gabriel Genellina

                #8
                Re: Coordinate Grid Points

                En Tue, 06 Feb 2007 20:35:43 -0300, <Eric.Gabrielso n@gmail.comescr ibió:
                Anyways heres my error:
                *************** *************** *************** *************** *************** *************** *************** ***************
                ***File "C:/Documents and Settings/Eric/Desktop/Python/2d guessing
                game.py", line 11, in <module>***
                *** randp = Point(random.ra ndint(1, 20), random.randint( 1,
                20)) ***
                ***TypeError: default __new__ takes no
                parameters
                ***
                class Point(object):
                def _init_(self, x, y):
                self.x = x
                self.y = y
                >
                #generate random coordinate point
                randp = Point(random.ra ndint(1, 20), random.randint( 1, 20))

                _init_ should be __init__ (two leading and trailing underscores)
                That special method is used to initialize your newly constructed Point
                instance.
                See section 9.3.2 on the Python tutorial:



                --
                Gabriel Genellina

                Comment

                • Eric.Gabrielson@gmail.com

                  #9
                  Re: Coordinate Grid Points

                  On Feb 6, 4:08 pm, "Gabriel Genellina" <gagsl...@yahoo .com.arwrote:
                  En Tue, 06 Feb 2007 20:35:43 -0300, <Eric.Gabriel.. .@gmail.comescr ibió:
                  >
                  >
                  >
                  Anyways heres my error:
                  *************** *************** *************** *************** *************** *************** *************** ***************
                  ***File "C:/Documents and Settings/Eric/Desktop/Python/2d guessing
                  game.py", line 11, in <module>***
                  *** randp = Point(random.ra ndint(1, 20), random.randint( 1,
                  20)) ***
                  ***TypeError: default __new__ takes no
                  parameters
                  ***
                  class Point(object):
                  def _init_(self, x, y):
                  self.x = x
                  self.y = y
                  >
                  #generate random coordinate point
                  randp = Point(random.ra ndint(1, 20), random.randint( 1, 20))
                  >
                  _init_ should be __init__ (two leading and trailing underscores)
                  That special method is used to initialize your newly constructed Point
                  instance.
                  See section 9.3.2 on the Python tutorial:http://docs.python.org/tut/node11.ht...00000000000000
                  >
                  --
                  Gabriel Genellina
                  WOW thank you so much I feel like a blind idiot *slaps self on
                  forehead*

                  Comment

                  • Matimus

                    #10
                    Re: Coordinate Grid Points

                    Code:
                    #classify "Point"
                    class Point(object):
                    def _init_(self, x, y):  <--- This is your problem
                    self.x = x
                    self.y = y
                    That should be '__init__'. Two underscores before and after. But,
                    IMHO, I don't think a class is really necessary unless you are going
                    to build in compare facilities. You might as well just use a tuple:

                    Code:
                    from random import randint
                    pt = (randint(1,20),randint(1,20))
                    ....
                    x,y = pt
                    x = pt[0]
                    y = pt[1]

                    Comment

                    Working...