7 Replies - 817 Views - Last Post: 31 August 2012 - 08:37 AM Rate Topic: -----

#1 Req4uim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 25-August 12

Proper use of If statements in relation to xcor and ycor

Posted 30 August 2012 - 06:49 AM

I'm working on a simple key movements with a tracer that I can turn on and off with a key. I've just disabled everything else. But I want to stop people who try to draw outside of the box[I'll settle for just the screen limits for now].

I know I've got to record x_coords and y_coords in a list on each movement of a key. Than from this list I have
to compare them to max(x,y,-x,-y). Am I appending in the right place? and are my if statements just nonsense as they keep resulting in the same error?

setup(screen_size,screen_size)
screen_size = 800
cursor_increment = 20
max_x_coord = (screen_size - cursor_increment)
max_y_coord = (screen_size - cursor_increment)
x_coords = []
y_coords = []
def movement_tracker():
    pos_x = xcor()
    pos_y = ycor()
    if pos_x() and pos_y() >- max_x_coord:
        goto((xcor()-cursor_increment),(ycor()-cursor_increment))
    if pos_x() and pos_y() <= -max_x_coord:
        goto((xcor()+cursor_increment),(ycor()+cursor_increment))
       
#This works
def movement_functions():    
    def directions(key, angle, distance=20):
        def onkey_movement_call():
            setheading(angle)
            fd(distance)
            stamp_pos_x = xcor()
            stamp_pos_y = ycor()
            x_coords.append(y_coords)
            y_coords.append(x_coords)
        onkey(onkey_movement_call,key)
    directions("Up", 90)
    directions("Down", 270)
    directions("Right", 360)
    directions("Left", 180)

#It results in this error.  
Traceback (most recent call last):
  File "C:\Users\User\Desktop\solution_update.py", line 104, in <module>
    movement_tracker()
  File "C:\Users\User\Desktop\solution_update.py", line 29, in movement_tracker
    if pos_x() and pos_y() <= -max_x_coord:
TypeError: 'int' object is not callable




Is This A Good Question/Topic? 0
  • +

Replies To: Proper use of If statements in relation to xcor and ycor

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Proper use of If statements in relation to xcor and ycor

Posted 30 August 2012 - 07:13 AM

Don't confuse functions and variables.
pos_x = xcor() # assigning value to pos_x from function call xcor()
if pos_x() # make function call to pos_x() and interpret result as boolean



I'm not sure what pos_x in your if actually means.

Perhaps:
if pos_x>=0 and pos_x < max_x_coord:
	# it's good



I'd also use pairs for much of what you're doing. e.g.
# x_coords = []
# y_coords = []
coords = []
....

# stamp_pos_x = xcor()
# stamp_pos_y = ycor()
stamp_pos = ( xcor(), ycor() )

# this is simply wrong.  Why add lists to each other? 
# x_coords.append(y_coords)
# y_coords.append(x_coords)

coords.append(stamp_pos)



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Proper use of If statements in relation to xcor and ycor

Posted 30 August 2012 - 11:33 AM

View Postbaavgai, on 30 August 2012 - 10:13 AM, said:

I'd also use pairs for much of what you're doing. e.g.
# x_coords = []
# y_coords = []
coords = []
....

# stamp_pos_x = xcor()
# stamp_pos_y = ycor()
stamp_pos = ( xcor(), ycor() )

# this is simply wrong.  Why add lists to each other? 
# x_coords.append(y_coords)
# y_coords.append(x_coords)

coords.append(stamp_pos)


Hope this helps.


This is the perfect opportunity to show you guys another awesome tool from my bag of tricks: The namedtuple!
from collections import namedtuple

#Create our own mini class called Point
Point = namedtuple("Point",['x','y'])

#Create a point from a set of coordinates
stamp_pos = Point(xcor(), ycor())

#get the x or y coordinate
print(stamp_pos.x)
print(stamp_pos.y)



I feel like this little abstraction can definitely bring a little extra clarity without a whole lot of effort... especially for big projects or when someone else will be looking at your code

This post has been edited by atraub: 30 August 2012 - 11:47 AM

Was This Post Helpful? 2
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Proper use of If statements in relation to xcor and ycor

Posted 30 August 2012 - 12:15 PM

As cute as namedtuple is, if I were using a class, I'd want a class. It's only a two liner, if you want to be stingy about it:
class Point(object): 
	def __init__(self, x, y): self.x, self.y = x, y




If you want a type of the fly, you can also do:
stamp_pos = type("Point", (object,), { 'x': xcor(), 'y': ycor() } )


Was This Post Helpful? 1
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Proper use of If statements in relation to xcor and ycor

Posted 30 August 2012 - 12:24 PM

but but but... the namedtuple is iterable and can be indexed... ok that's kind of pointless and stupid but it makes some people happy.

On a serious note, it does allow the tuple to be unpacked, which can be kinda nice at times.
def doSomethingWithCoords(x, y):
    print(x)
    print(y)

>>> a = Point(6,6)
>>> doSomethingWithCoords(*a)
6
6

Admittedly,it's not the most compelling thing ever, but it does have some little niceties.

On a side note, that class declaration is 2 lines without semi colons, but it really feels like something that should be expressed over 4 lines.

This post has been edited by atraub: 30 August 2012 - 12:33 PM

Was This Post Helpful? 2
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Proper use of If statements in relation to xcor and ycor

Posted 30 August 2012 - 12:34 PM

Which, honestly, brings me back to the idea of a plain old tuple for this one.
>>> point = (5,6)
>>> point
(5, 6)
>>> 
>>> doSomethingWithCoords(*point)
5
6
>>> 



I mean, it looks like a point! :P

One of the things I like about Python is being able to throw complex data around without all the baggage that other languages carry. I agree, sometimes you want nice, neat, class types. But often you just want a mellow Pythony contract.
Was This Post Helpful? 1
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Proper use of If statements in relation to xcor and ycor

Posted 30 August 2012 - 12:48 PM

The nice thing is that you can do point.x or point.y which is a little more clear than point[0] or point[1], but you still maintain all the cool stuff you get from using tuples :)

namedtuples still have a special place in my heart but I do understand why you might not share my fondness for them.

This post has been edited by atraub: 30 August 2012 - 12:50 PM

Was This Post Helpful? 2
  • +
  • -

#8 Req4uim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 25-August 12

Re: Proper use of If statements in relation to xcor and ycor

Posted 31 August 2012 - 08:37 AM

Thanks once again guys. My mind was a bit slow last night and I see where I went wrong finally got everything working !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1