2014-02-10 137 views
0

我的龜已經死了!他不會使用我的循環。 nmbrOfSides正在重新調整3順便說一句。有關我可能會忽略的任何提示?我的龜已經死了

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() 

回答

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

那不是做你認爲它。您需要調用的函數,而不是爲他們分配:

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

粗略地說,你說的是烏龜「轉發50個像素」,而不是「前進50個像素」,和烏龜會重新定義其概念「前進」意味着什麼,而不是移動。

+0

謝謝你這麼多 – user3291152

0

Python從左到右評估等式,但某些操作具有優先權(類似於PEMDAS)。

您的模數操作正在求和之前進行評估。嘗試用括號來計算它:

sides = (3 + student_number) % 3 

這對於student_number=0應該返回1

+0

'(3 + student_number)%3'完全等同於'student_number%3',和我很確定這不是提問者想要的。我認爲這一行動的順序按預期工作,選取3到5之間的值。 – Blckknght