Python Forum

Full Version: i need help with an error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Hello,

I am new in python and I don't know why I gen an error when I try to test my code. If someone can help me I will be really grateful  Big Grin .

I am using python 3.6.1 and I saved the code with name testosa.py. When I hit run module(f5) an error occurs(Syntax error - invalid syntax - Python 3.6.1).

What can I do so I can test my code?
I can't post images yet.

I will copy-paste my code:

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.

>>> import turtle
>>> my_turtle = turtle.Turtle()
>>> my_turtle.speed(0)
>>> def square(lenght,angle):
for i in range(4):
my_turtle.forward(lenght)
my_turtle.right(angle)


>>> for i in range(150):
square(100,90)
my_turtle.right(11)


>>>
we don't want images anyway.
however you need to put your code in code tags, as I told you. Make sure to preserve the indentation as is. Indentation matter in Python.
Also, post the full Traceback (the error message) in error tags. It is informative what the problem is and at/around which line it happened.

As is your code now we are not able to tell anything.

As a side note - it is better to write your code and save it as .py file instead of writing it in the interactive prompt, like you do now. This way it will be easier for you to rerun the program, debug, etc.
A popup with name SyntaxError appear and the only error message is: invalid syntax(I have no other messages, neither in shell or popup). The 6 from first line (Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32) turns red (this is the only difference between original shell with my code and the new one after I hit Run Module).

The code works when I hit enter after I write it. After that I am saving the code as a .py file. When I open it again and when I hit run - Run Module I got that error.

Now I hope that my code appears right.

>>> import turtle
>>> my_turtle = turtle.Turtle()
>>> my_turtle.speed(0)
>>> def square(lenght,angle):
	for i in range(4):
		my_turtle.forward(lenght)
		my_turtle.right(angle)

		
>>> for i in range(150):
	square(100,90)
	my_turtle.right(11)

	
>>> 
import turtle
my_turtle = turtle.Turtle()
my_turtle.speed(0)
def square(lenght,angle):
    for i in range(4):
        my_turtle.forward(lenght)
        my_turtle.right(angle)
 
         
for i in range(150):
    square(100,90)
    my_turtle.right(11)
 
when you write your file it should like this above. everything else is output from the interactive prompt, it's not part of the code.
You can write the code in your favorite text editor or more advanced IDE. For start you can use also IDLE, but it's not considered good IDE
no indent on 1st for loop
length spelling wrong, but done twice so ok

call to square and my_turtle.right not indented
code should look like:

>>> import turtle
>>> my_turtle = turtle.Turtle()
>>> my_turtle.speed(0)
>>> def square(lenght,angle):
        for i in range(4):
            my_turtle.forward(lenght)
            my_turtle.right(angle)
 
         
>>> for i in range(150):
        square(100,90)
        my_turtle.right(11)
trampled on buran ... sorry, typing at same time
@Larz60+ note that OP writes the code in interactive prompt, then save everything and get error for the following:

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.

he claims it runs OK first time, so I assume in reality his indentation is OK
Before I save as a .py file the code works(I get a circle made of squares ). I tried again with your code, same error. I installed python 3.5 and still the same error. And I save it as a .py file for sure.
Which code did you try? Mine or Larz60+? Note he left the >>> from the interactive prompt. You need to remove these in the .py file.
If you try something and report back - post always the code you tries AND the Traceback. So far you didn't show us any traceback.
Pages: 1 2 3