Reputation: 19
I am python beginner and when I type in this code the error says "can't assign to literal" how do I fix this?
from turtle import*
from math import*
def triangle():
triangle=(int(bodylength))/5*3/2/cos(70)
penup()
forward(int(bodylength)/5)
pendown()
left(70)
forward(int('triangle'))
right(140)
forward(int('triangle'))
forward(int(bodylength)/5)
triangle()
Upvotes: 0
Views: 237
Reputation: 41872
Although changing forward(int('triangle'))
to forward(int(triangle))
is necessary, it's not sufficient as bodylength
is undefined. Let's rework it to get make the code clearer and run:
from turtle import Turtle, Screen
import math
def triangle(turtle, bodylength):
distance = bodylength / 5 * 3 / 2 / math.cos(math.radians(70))
turtle.penup()
turtle.forward(bodylength / 5)
turtle.pendown()
turtle.left(70)
turtle.forward(distance)
turtle.right(140)
turtle.forward(distance)
turtle.forward(bodylength / 5)
screen = Screen()
yertle = Turtle()
triangle(yertle, int(input("Body length: ")))
screen.exitonclick() # depending on the environment, you may not need this
Upvotes: 1
Reputation: 640
intead of using 'triangle'
use just triangle
so your code will look like this:
from turtle import*
from math import*
def triangle():
triangle=(int(bodylength))/5*3/2/cos(70)
penup()
forward(int(bodylength)/5)
pendown()
left(70)
forward(int(triangle))
right(140)
forward(int(triangle))
forward(int(bodylength)/5)
triangle()
You need to do this because 'triangle'
to python is a String not a variable. To find out more about variables and strings, just google what is the difference between strings and variables
Note: Looks like there may be other errors to your code like forward()
. You might need to call turtle.forward()
or something. And I also think that Math.cos()
takes rad instead of deg for the parameter.
Upvotes: 0
Reputation: 48572
The problem is your statement 'triangle'=(int(bodylength))/5*3/2/cos(70)
. You can't set a string literal to be equal to something else. If you meant for that to be a variable, you need to remove the quotes from around it. Also, you shouldn't have a variable with the same name as a function.
Upvotes: 0