0

what's the problem in the code below? It only shows two arrows when I run it Of course, the first was the import thread, but because it gave an error (no module named 'thread'), I changed it to import threading

import threading
import turtle

def f(painter):
    for i in range(3):
        painter.fd(50)
        painter.lt(60)

def g(painter):
    for i in range(3):
        painter.rt(60)
        painter.fd(50)

try:
    pat=turtle.Turtle()
    mat=turtle.Turtle()
    mat.seth(180)
    thread.start_new_thread(f,(pat,))
    thread.start_new_thread(g,(mat,))
    turtle.done()

except:
    print("hello")

while True:
    pass

enter image description here

Lara
  • 31
  • 6
  • 2
    If you imported ```threading```, what is ```thread``` –  Jul 26 '21 at 13:53
  • Instead of thread, I wrote threading, so that the program doesn't give an error, but now that I have changed it, the program only shows two arrows in execution and doesn't move. Please try the code yourself to understand better – Lara Jul 26 '21 at 14:02
  • Turtle doesnt like multithreading! Look here https://stackoverflow.com/questions/19498447/multithreading-with-python-turtle – FloLie Jul 26 '21 at 14:07
  • Don't wrap try/except around blocks like that; you're missing the traceback saying `NameError: name 'thread' is not defined` due to the rename! a static linter like flake8 will help you find these! – ti7 Jul 26 '21 at 14:18
  • I went to a sending link and tried the suggested libraries, but it still gives only two flash drives and does not move – Lara Jul 26 '21 at 14:20

1 Answers1

0

threaing and thread are separate modules to deal with threads in python. However thread module is considered as deprecated in python3 but renamed to _thread for backward compatibility. In your case i assume you are trying to use _thread module with python3.

So your code should be as follows.

import _thread
import turtle

def f(painter):
    for i in range(3):
        painter.fd(50)
        painter.lt(60)

def g(painter):
    for i in range(3):
        painter.rt(60)
        painter.fd(50)

try:
    pat=turtle.Turtle()
    mat=turtle.Turtle()
    mat.seth(180)
    _thread.start_new_thread(f,(pat,))
    _thread.start_new_thread(g,(mat,))
    turtle.done()
except:
    print("Hello")

while True:
    pass

since _thread module is deprecated, it is better you move to threading module.

Sahan
  • 176
  • 5
  • Thanks very much, it's work. Did _thread come in place of the obsolete thread? Or do we add an underline to access to all deprecated libraries? – Lara Jul 26 '21 at 14:43
  • What changes do I need to make to the code if I want to run the code by threading? – Lara Jul 26 '21 at 15:42
  • _thread is the same module as thread and only the name is renamed since it is deprecated. instead of _thread module use threading module. I am not going to talk much about threading module and you can learn about it [here](https://docs.python.org/3/library/threading.html). And [here](https://realpython.com/intro-to-python-threading/#working-with-many-threads) is a great tutorial for beginners written by Dan. Please mark my answer as correct if it is useful. – Sahan Jul 27 '21 at 01:55