-4
import time
from threading import *
class MyThread(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        for el in range(10):
            print self.getName(), el
            time.sleep(0.01)

thread1 = MyThread()
thread2 = MyThread()
Thread3 = MyThread()
Thread4 - MyThread()



----------------------------------------
  File "C:\Users\Happy\PycharmProjects\crawling\test.py", line 9
    print self.getName(), el
          ^
SyntaxError: invalid syntax

Process finished with exit code 1
tdelaney
  • 73,364
  • 6
  • 83
  • 116
Andy Kim
  • 5
  • 3
  • 1
    If you are using python 3, `print()` is a function. You need to put the arguments in `()`. `print( self.getName(), el)`. Also, your indentation seems to be wrong. This is important in python. – Mark Aug 05 '21 at 05:23
  • 2
    In your own words, what do you think the code `print self.getName(), el` should do? Where did you learn it? What version of Python are you using, and what version of Python does that tutorial say it is for? What happened when you tried putting `python print` into a search engine? – Karl Knechtel Aug 05 '21 at 05:24
  • I fixed indentation errors that I assume were just stackoverflow editing problems. Let me know if I should revert to the original. – tdelaney Aug 05 '21 at 05:32
  • Try `import sys` and then `print(sys.version)`. This will properly print python version in 2.x and 3.x. In 3.x, its now `print("whatever")`. If you are following a tutorial that shows `print self.getName(), el`, then its a 2.x tutorial that is horribly out of date and you shouldn't waste your time with it. – tdelaney Aug 05 '21 at 05:35
  • 1
    @tdelaney the point is that, by answering the questions I ask, OP will find the correct answer and also practice important problem-solving skills. It's really hard nowadays to find a 2.x tutorial that isn't explicitly marked as being for 2.x. Any number of obvious web searches will also find relevant information, since all the top hits are for 3.x now (by a huge margin). – Karl Knechtel Aug 05 '21 at 05:40
  • Does this answer your question? [Syntax error on print with Python 3](https://stackoverflow.com/questions/826948/syntax-error-on-print-with-python-3) – Gino Mempin Aug 05 '21 at 05:49

1 Answers1

2

In Python 3, print is a function, so you have to use parentheses like this:

import time
from threading import *
class MyThread(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        for el in range(10):
            print(self.getName(), el) # watch the change on this line
            time.sleep(0.01)

thread1 = MyThread()
thread2 = MyThread()
Thread3 = MyThread()
Thread4 = MyThread()
Yuval.R
  • 1,182
  • 4
  • 15