0

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!

2
  • What do you have difficulties with? Commented Oct 6, 2015 at 20:02
  • "This won't work" isn't a question, nor is it fit for StackOverflow. Try elaborating on what the output is, compared to what you expected. What have you tried to fix the error? Commented Oct 6, 2015 at 20:05

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@TechyEngineer Re: If this answers your question, approve the answer ;)
At the very bottom, after the function?
Yes. To be clear, you need to define a function before calling it... but if you don't call it you'll never actually run it. Using standard library or 3rd party functions works because they've been defined elsewhere and imported.
1

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

Comments

0

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: "))

Comments

0

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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.