473,578 Members | 3,102 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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://
http://www.cs.washington.edu/educati...07wi/homework/
homework.html if you would like to see a more in depth discription of
the game.

Many Thanks,
Eric

Feb 5 '07 #1
9 5401
Er************* @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://
http://www.cs.washington.edu/educati...07wi/homework/
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
Feb 5 '07 #2
Er************* @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://
http://www.cs.washington.edu/educati...07wi/homework/
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.
Feb 5 '07 #3
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://
http://www.cs.washington.edu/educati...07wi/homework/
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

Feb 6 '07 #4
Er************* @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://
http://www.cs.washington.edu/educati...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
Feb 6 '07 #5
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://
http://www.cs.washington.edu/educati...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
*************** *************** *************** *************** *************** *************** *************** ***************

Feb 6 '07 #6
Er************* @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
Feb 7 '07 #7
En Tue, 06 Feb 2007 20:35:43 -0300, <Er************ *@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

Feb 7 '07 #8
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*

Feb 7 '07 #9
Expand|Select|Wrap|Line Numbers
  1. #classify "Point"
  2. class Point(object):
  3. def _init_(self, x, y):  <--- This is your problem
  4. self.x = x
  5. self.y = y
  6.  
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:

Expand|Select|Wrap|Line Numbers
  1. from random import randint
  2. pt = (randint(1,20),randint(1,20))
  3. ....
  4. x,y = pt
  5. x = pt[0]
  6. y = pt[1]
  7.  
Feb 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6064
by: Ashutosh Iddya | last post by:
HI, How do you find the other endpoint of a line given one endpoint and the slope of the line. Also how do you display the coordinate on the computer screen knowing that the Y axis (of the raster display) runs in the reverse direction of a normal graph.
2
6096
by: PhilC | last post by:
Hi Folks, I'm trying to click on a canvas to draw a line. The canvas has scroll bars. All is well until I move the scrollbars. This naturally because the relationship between my screen click and canvas event.x, event.y has changed. I need something that will add the proportional amount the scroll bar has moved to the mouse click...
8
2562
by: Piotre Ugrumov | last post by:
How can I implement a function that calculate the coordinates of a point, in 3 dimensional space? The function take 6 value (x, y, z, v(velocity), corner alfa, corner beta). The prototype is this: double coordinate(double x, double y, double z, double c, double alfa, double beta); How can I implement a function that return the corner...
1
1570
by: Rob Richardson | last post by:
Greetings! I see from the group archives that a lot of people don't like how Snap to Grid works when placing controls on forms. I am now among that number. When I had the Windows Forms Designer option for snap to grid set to False and the form's SnapToGrid and DrawGrid properties left at their default settings, I could hold down Ctrl and...
2
4980
by: SStory | last post by:
I have a picture that I allow the user to move around and would like to offer grid support like VB and other apps so that it will snap to the nearest point. How can I offer grid support so that the image will snap in place on grid points like a commerical app? I am not sure how to implement that. Anyone have any code? Thanks,
13
2647
by: Guardiano del Faro | last post by:
Hello!! i'm just a dummie in C/C++ and i need you help for solving my little problem. I have to create a 3D grid of points. Each point has the 3 coordinates and a boolean value. (I made a structure in order to make this). My task is to scan the grid through the x, y and z planes.
1
10456
by: psbasha | last post by:
Hi , I have a list of points and I have to find the boundary nodes from the points. For example : pointList = ,,,,] How to sort the coordinate values in the list based on X:Xmin,Xmax Y:Ymin,Ymax,Z: Zmin and Zmax?.Is there any method or function available in Python to sort the list values based on the X ,Y and Z values
12
18334
by: Petra Rohmer | last post by:
Hello, I want to ake following 0,0 (900mm,900mm) -------------------------------------- |....................................| |....................................| |....................................|
1
1507
by: Tyler Eaves | last post by:
Hi, wondering if anyone can help me with this. I'm trying to do some math for a reasonably simple bit of 3d math. Given the following. A 3d vector pointing in an arbitrary direction, call it 'forward', that defines the target Z-axis, and a rotation around that vector, 'roll', which defines the target Y axis. These define the 'target'...
0
7847
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7778
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8125
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8148
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6522
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5342
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3798
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1389
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1113
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.