Question

My Turtle is DEAD!!! He won't draw using my loop. nmbrOfSides is retuning 3 BTW. Any hints on what I may be overlooking?

import turtle
counter = 0

def poly_side_calculation (student_number):
    sides = 3 + student_number % 3
    return sides ####function returns 3 when I enter student number



print("welcome to my TURTLE demonstration.\n")

studentNmbr = int(input("Please enter your student number to get started: " ))
print("Here are your choices for the color of your polygon. You can choose red, green,    blue, yellow, cyan, magenta, black or white.")
fill_color = input("Please enter the color that you would like your polygon to be: ")
poly_side_length = int(input("Please enter the length of your polygon side: "))

nmbrOfSides = poly_side_calculation(studentNmbr)
vertex_angle = 360 / nmbrOfSides

turtle.color("black", fill_color)
turtle.pensize(5)

turtle.showturtle()
turtle.pendown()
turtle.begin_fill()
turtle.setpos(-150,150)
while counter <= nmbrOfSides:
    turtle.forward = (poly_side_length)
    turtle.left = vertex_angle
    counter = counter + 1
turtle.end_fill()
Était-ce utile?

La solution

turtle.forward = (poly_side_length)
turtle.left = vertex_angle

That isn't doing what you think it does. You need to call the functions, not assign to them:

turtle.forward(poly_side_length)
turtle.left(vertex_angle)

Roughly speaking, you were telling the turtle "Forward is 50 pixels" instead of "Go forward 50 pixels", and the turtle was redefining its concept of what "forward" means instead of moving.

Autres conseils

Python evaluates equations from left to right, but some operations have precedence (something like PEMDAS).

Your modulus operation is being evaluated before the sum. Try to compute it with parenthesis:

sides = (3 + student_number) % 3

Which for student_number=0 should return 1

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top