Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

'forward' is not defined

Sorry for dumb question, i’m new to python

What i’ve writed in code:

import time
from random import randint
from turtle import *
while True:
    if randint(1,2) == 1:
        forward(randint(20,100))
    else:
        backward(randint(20,100))
    if randint(1,2) == 1:
        right(randint(20,100))
    else:
        left(randint(20,100))

What i’ve got in output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  File "C:\Users\amogus\Desktop\python\turtle.py", line 4, in <module>
    from turtle import *
  File "C:\Users\amogus\Desktop\python\turtle.py", line 7, in <module>
    forward(randint(20,100))
NameError: name 'forward' is not defined

Can someone help with this?

>Solution :

Your python file is named turtle.py, so when you type from turtle import * it attempts to import everything from itself, and since there’s nothing defined yet, it’s essentially a no-op (i.e. doing nothing).

Rename your python file to anything but turtle.py and it should work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading