Reputation: 31
I want to use python turtle to create a program that asks the user how many sides they want on a polygon, then turtle draws it. Here is my code:
import turtle
window = turtle.Screen()
window.bgcolor("lightgreen")
shape = turtle.Turtle()
shape.color("blue")
shape.pensize(3)
sides = int(input("How many sides do you want? Use digits: "))
def polygon(sides,length):
for x in range(sides):
shape.forward(length)
shape.left(360/sides)
For some reason this won't work. Any ideas?
Thanks in advance!
Upvotes: 0
Views: 5894
Reputation: 3
Your code should look like this
import turtle
window = turtle.Screen()
window.bgcolor("lightgreen")
shape = turtle.Turtle()
shape.color("blue")
shape.pensize(3)
sides = int(input("How many sides do you want? Use digits: "))
def polygon(sides, length):
for x in range(sides):
shape.forward(length)
shape.left(360/sides)
polygon(10, 90) # replace these numbers with the values that you want
The first thing I saw was you didn't have a space between the two variables in the def() statement. The second thing is that when you create a def it is like creating a big variable where all the code inside is that variable(kinda) the variables inside the parenthesis in the def statement are variables that need to be defined later on when calling the function so that the function will run correctly!
Upvotes: 0
Reputation: 85
The only reason i can see why it doesn't work is that you haven't put in the final line of code in. This is actually essential or python wont run the def. For your instance it would be: polygon(sides,100) I only put the 100 in as an example and you can change it yourself to whatever you desire
Upvotes: 1
Reputation: 189
You need to call the function you create like this:
polygon(sides,100)
Or any other length you want instead of 100
The other thing you can do is to ask user to enter the length from console
length = int(input("How tall do you want the lines? Use digits: "))
Upvotes: 0
Reputation: 12773
You don't actually call polygon
, you only define it (that's what the def
part of def polygon(sides,length):
means.
Try adding something like
polygon(sides, length)
to the bottom of your script; or more specifically anywhere after the definition.
Original
If you're using Python 2 you should probably use raw_input
instead of input
.
Other than that, try and include the error message / output to receive a moore targeted answer.
Upvotes: 3