2

I can't find the answer as why this code for range loop, behaves OK in 2.7 and not in 3.4.

from graphics import *


def main():
    win = GraphWin("Click Me!")
    for i in range(10):
        p = win.getMouse()

        print("you clicked at:", p.getX() , p.getY())


 main()

output on 2.7 is: "you clicked at: 56 77 " once I click

output on 3.4 is" "you clicked at:" once I click then, 56 77 "you clicked at:

Is it range behaving different or print?

3
  • 1
    This is a pretty straight forward question, that includes code and a clear description of the problem. Its not worth the downvote brigade. Commented Jan 8, 2015 at 1:01
  • Perhaps in 3.4 you're getting both a down-click and an up-click event. Commented Jan 8, 2015 at 1:35
  • Well it behaves like this one down + up event = half of what i have inside print. one more time and prints p.get(), p.gety() in the same line but also prints the next "you clicked at:" in the next line. Also if you remove the loop and make it work for once it works. But i can't figure out why this behaviour. Commented Jan 8, 2015 at 1:42

1 Answer 1

2

In Python 2 print is a statement, and this:

print("you clicked at:", p.getX() , p.getY())

prints the tuple ("you clicked at:", p.getX() , p.getY()), which my default puts a space between each entry.

In Python 3, print is a function and you are calling it with the arguments:

"you clicked at:", p.getX() , p.getY()

As the documentation shows:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)


Addendum:

After some research on the GraphWin, it appears that you are using the Zelle Graphics library. Which has been updated once since 2011 to correct a small 2-to-3 issue, and some universities appear to be using a much older version.

I didn't delve much deeper in but I think that there is going to be a few incompatibilities in the library so may not be able to do a straight switch to Python 3.

Sign up to request clarification or add additional context in comments.

5 Comments

But why when i remove the for i in range(10) in python 3, the output is right in one line?
I'm not quite sure why that is, but the range function isn't causing your problem, its that you are using print incorrectly.
I don't think that's the problem: sep and end have to be keyword arguments, don't they? From the docs: All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end.
I understand i did it in python 2, i treated it as a function. But again in Python 3 once i remove the for range loop the output is different and i think print is being used property, like xnx said shouldnt the keyword arguments be preceded by an end and sep?
Thanks for your help, I think you might be right about Zelle's module. But i still find strange that by removing the loop makes it work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.