Hello,
I am very new to python (a few days) and am wondering how i should go about generating random gridpoints and comparing them to ones inputted by the user. I am completely lost on how i can get the input of the x and y coordinates from the user and compare them to the ones that were randomly generated earlier. This is how I think I should be generating the random coordinate points (most likely wrong or inefficient way to do so)

[LIST]
[*]import random # import random module
[*]#define coordinate point function
[*]def coordp(x, y):
[*]    return(x, y)
[*]#declare randcoord var
[*]randcoord = 0
[*]#generate random number
[*]randcoord = coordp(random.randrange(1,21,1),random.randrange(1,21,1))[/LIST]

Recommended Answers

All 8 Replies

This is how you can create a list of random (x, y) points ...

import random as rn
# a list of 5 random (x, y) points
rp_list = []
for k in range(5):
    rp_list.append((rn.randint(1, 21), rn.randint(1, 21)))
print "Random point list:"
print rp_list  # eg. [(19, 12), (20, 13), (15, 2), (11, 19), (20, 14)]

Now let the user input a number of (x, y) points ...

# user inputs 5 (x, y) points
up_list = []
for k in range(5):
    x_prompt = "Enter x%d = " % (k+1)
    y_prompt = "Enter y%d = " % (k+1)
    x = int(raw_input(x_prompt))
    y = int(raw_input(y_prompt))
    up_list.append((x, y))
print "User point list:"
print up_list  # eg. [(7, 21), (10, 18), (19, 12), (12, 18), (4, 9)]

Last not least compare the two lists ...

# compare 2 lists
rp_list = [(19, 12), (20, 13), (15, 2), (11, 19), (20, 14)]
up_list = [(7, 21), (10, 18), (19, 12), (12, 18), (4, 9)]
for p in rp_list:
    if p in up_list:
        print p,"in both lists"

Try it, study it, experiment with it ...

Thank you for your help, but the reason that i want this is because I am working on a game where:

1st a random coordinate point is generated ( from 1,1 to 20,20)
2nd a user inputs a guess (from 1,1 to 20,20)
3rd the users inputted guess is compared to the randomly generated one and is responded to with a hint (if x value too low it says go east, if y value too high it says go south if combination southeast etc.
4th repeat steps 2 and 3 until correct coordinate point is inputted

and so I am wondering if there are specific ways in which i can access the x value from both rp_list and up_list and in if statements do the following for example

"if rp_list.x > up_list.x
print "Go north"
elif rp_list.x < up_list.x
print "Go south""

etc

Okay, now we are on the same wavelength ...

# coordinate points (x, y)
# test out the if/elif stuff well!!!
 
import random as rn
 
class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
 
# generate random point rnp
rnp = Point(rn.randint(1, 20), rn.randint(1, 20))
# test
print rnp.x, rnp.y
 
# get user point usp
# x is west to east
# y is south to north
x = int(raw_input("Enter west/east   x (1 -20): "))
y = int(raw_input("Enter south/north y (1 -20): "))
attempts = 0
while True:
    usp = Point(x, y)
    attempts += 1
 
    if usp.y == rnp.y and usp.x == rnp.x:
        print "You found it!!!"
        break    
 
    elif usp.y == rnp.y:
        if usp.x > rnp.x:
            print "Go west"
        elif usp.x < rnp.x:
            print "Go east"
        x = int(raw_input("1Enter west/east   x (1 -20): "))
        usp = Point(x, y)
 
    elif usp.y < rnp.y:
        if usp.x > rnp.x:
            print "Go northwest"
        elif usp.x < rnp.x:
            print "Go northeast" 
        x = int(raw_input("2Enter west/east   x (1 -20): "))
        y = int(raw_input("2Enter south/north y (1 -20): "))
        usp = Point(x, y)
 
    elif usp.y > rnp.y:
        if usp.x > rnp.x:
            print "Go southwest"
        elif usp.x < rnp.x:
            print "Go southeast"
        x = int(raw_input("3Enter west/east   x (1 -20): "))
        y = int(raw_input("3Enter south/north y (1 -20): "))
        usp = Point(x, y)
 
    elif usp.x == rnp.x:
        if usp.y > rnp.y:
            print "Go south"
        elif usp.y < rnp.y:
            print "Go north"
        y = int(raw_input("4Enter south/north y (1 -20): "))
        usp = Point(x, y)
 
print "attempts =", attempts

Didn't have much time, so try, correct and improve the if/elif stuff as needed!

Looks like someone at the office has been messing with the code field again, sorry!
Copy and pasting code is a mess now!!!!!!!!!!!

Looks like someone at the office has been messing with the code field again, sorry!
Copy and pasting code is a mess now!!!!!!!!!!!

Gee, vegaseat you are old and blind. This is supposed to be improvement. See the little line 'Toggle Plain Text' just below the code, click on it, the line numbers are gone and now you can highlight, copy and paste as usual. Merry coding!
Your friend Henri

Thank you very much for the help you have given me so far (im pathetically helpless) i understand everything but the whole defining the "point" class and intend to go read up on it. but i now have an error and am wondering if you could point me in the right direction

first of all this is my error:
File "C:/Documents and Settings/Eric/Desktop/Python/2d guessing game.py", line 11, in <module>
randp = Point(random.randint(1, 20), random.randint(1, 20))
TypeError: default __new__ takes no parameters

and this is 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.randint(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

Ah, a common mistake made by beginners, the class constructor method __init__() has two leading and trailing underlines! If it doesn't find the constructor it uses __new__() and that does not take any arguments.

Member Avatar for Mouche

You need two underscores on both sides of the "init" method of your Point class.

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

pt1 = Point(1,4)
print "pt1 x = %d; pt1 y = %d" % (pt1.x,pt1.y) # gives "pt1 x = 1; pt1 y = 4"

This class works.

edit: whoops. looks like vegaseat is too fast for me!

You need two underscores on both sides of the "init" method of your Point class.

#classify "Point"
class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
 
pt1 = Point(1,4)
print "pt1 x = %d; pt1 y = %d" % (pt1.x,pt1.y) # gives "pt1 x = 1; pt1 y = 4"

This class works.

edit: whoops. looks like vegaseat is too fast for me!

Thanks LaMouche, that is not an easy mistake to detect, particularly with my bad eye sight!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.