Ask Your Question
0

python syntax error for rosrun .py script

asked 2017-12-30 10:07:11 -0500

KevinH gravatar image

updated 2017-12-30 11:54:18 -0500

gvdhoorn gravatar image

Hello I'm a noob at python, I downloaded this github package (turtlesim_hunter) Then when I rosrun I get a syntax error:

knives@knives:~/catkin_ws/src/turtlesim_hunter/src/turtlesim_hunt/Scripts$ rosrun turtlesim_hunt hunt.py
  File "/home/knives/catkin_ws/src/turtlesim_hunter/src/turtlesim_hunt/Scripts/hunt.py", line 151
    print "Found #" + str(huntedTurtles) + " after " + str(finalTime) + " Current Avg: " + str(totalHuntTime / huntedTurtles)
                  ^
SyntaxError: invalid syntax

The issue is at line 151 in the hunt.py script:

https://github.com/tiedyedguy/turtles...

print "Found #" + str(huntedTurtles) + " after " + str(finalTime) + " Current Avg: " + str(totalHuntTime / huntedTurtles)

I don't know whats wrong and I can't google myself to a specific answer, is the issue that this was written with python 3 and I only have python 2.7 installed? I highly doubt this guy has an error in his code since it's working for him:

https://www.youtube.com/watch?v=LywSO...

edit retag flag offensive close merge delete

Comments

1

Try commenting out that line (put a # in front of it), and if you want to see what it was printing gradually restore it - just print the huntedTurtles bit and put the hash after but before the plus sign with the rest of it, then move the hash to the right so more and more of the line will print

lucasw gravatar image lucasw  ( 2018-01-02 12:20:32 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2018-01-03 11:44:00 -0500

Try enclosing your print parameters in brackets, this fixed the problem for me:

print ("Found #" + str(huntedTurtles) + " after " + str(finalTime) + " Current Avg: " + str(totalHuntTime / huntedTurtles))

Hope this helps.

edit flag offensive delete link more
0

answered 2020-04-24 00:03:24 -0500

If you write print() function in a program and someone using Python 2.x tries to run it, they will get an error. To avoid this, it is a good practice to import print function :

from __future__ import print_function

The from __future__ import print_function ; to bring the print function from Python 3 into Python 2.x. Now your code works on both Python 2.x and Python 3.x . The __future__ statements need to be near the top of the file because they change fundamental things about the language, and so the compiler needs to know about them from the beginning.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-12-30 10:07:11 -0500

Seen: 612 times

Last updated: Jan 03 '18