Python Turtle向日葵花瓣颜色循环异常:第四瓣变黑求助
Python Turtle向日葵花瓣颜色循环异常修复
问题情况
使用Python Turtle库绘制向日葵,花瓣要求按橙色、黄色、橙色、黄色规律循环,但第四片花瓣始终显示为黑色,尝试tur.pencolor('yellow')无效,其余功能正常。
原代码
import turtle # Define function draw_petal def draw_petal(tur, speed, n_sided, radius): """ Draws a petal of a sunflower using turtle graphics. Parameters: - tur (turtle.Turtle): The turtle object used for drawing. - speed (int): The drawing speed of the turtle. Accepts standard turtle speed inputs. - n_sided (int): The number of sides of the petal, indicating its shape. - radius (float): The radius of the petal, measured from the center to any vertex. The function draws a petal as an n-sided polygon with the specified radius. The petal shape is determined by the number of sides (n_sided) and its size by the radius. Note: The turtle will start drawing from its current position and heading. """ # TODO: Remove pass and provide your own code below angle = 360 / n_sided for i in range(n_sided): tur.forward(radius) tur.left(angle) # Define fuction draw_sunflower def draw_sunflower(tur, speed, n_sided, radius, num_petal): """ Draws a sunflower using turtle graphics. Parameters: - tur (turtle.Turtle): The turtle object used for drawing. - speed (int): The drawing speed of the turtle. Accepts standard turtle speed inputs. - n_sided (int): The number of sides for each petal, indicating the shape of the petal. - radius (float): The radius of each petal, measured from the center to any vertex. - num_petal (int): The total number of petals to draw for the sunflower. The function will alternate petal colors between 'orange' and 'yellow'. The heading angle and current color of each petal are printed to the console during the drawing process for diagnostic purposes. Note: This function assumes a helper function `draw_petal` is defined elsewhere to draw individual petals. """ # TODO: Remove pass and provide your own code below tur.speed(speed) angle_increment = 360 / num_petal c = tur.color() for i in range(num_petal): draw_petal(tur, speed, n_sided, radius) if(i % 2 == 0): tur.color("yellow") if(i % 2 == 1): tur.color("orange") # Diagnostic printout print(f"Current color: {c}, heading angle {tur.heading()} degrees") tur.left(angle_increment) # Define the main() function for drawing sunflowers using turtle graphics. # # This function sets up a turtle environment and prompts the user # for inputs to draw a sunflower. The user can choose the animation # speed, the number of sides for a petal, the radius of petals, and # the total number of petals. # # After drawing a sunflower based on the user's input, the program # asks the user if they'd like to draw another sunflower. If the # user enters "yes", the screen will be cleared and the user will be # prompted for new input values. If the user enters "no", a message # will be displayed prompting the user to click on the window to exit. # # If the user enters any other command, an error message is printed # and the user is prompted again. # # The function concludes by waiting for the user to click on the turtle # window, at which point it will close the window and exit the program. def main(): screen = turtle.Screen() screen.title("Sunflower Drawing") tur = turtle.Turtle() while True: # User input speed = int(input("Enter animation speed: ")) n_sided = int(input("Enter the number of sides of a petal: ")) radius = float(input("Enter the radius of petals: ")) num_petal = int(input("Enter the number of petals: ")) # Draw the sunflower draw_sunflower(tur, speed, n_sided, radius, num_petal) # Ask if the user wants to draw another sunflower response = input("Draw Another Sunflower?") if response == "yes": tur.clear() tur.penup() tur.home() tur.pendown() elif response == "no": tur.penup() tur.goto(0, -200) tur.write("Click window to exit...", align="right", font=("Courier", 12, "normal")) break turtle.exitonclick() else: print("Invalid command! Please try again…") screen.mainloop() # Run the program if __name__ == "__main__": main() # turtle.exitonclick()
问题根源
- 颜色设置顺序颠倒:先画花瓣再设置颜色,导致第一片花瓣使用海龟默认的黑色,后续颜色始终延迟一步。
- 颜色循环逻辑与要求相反:要求橙→黄→橙→黄,当前代码i为偶数时设黄色,奇数时设橙色,顺序完全搞反。
- 调试信息无效:
c = tur.color()只在函数开头获取一次初始颜色,后续打印的都是旧值,无法反映当前实际颜色。
修复后的代码
重点修改draw_sunflower函数,调整颜色设置顺序和逻辑,同时完善初始化与重置逻辑:
import turtle # Define function draw_petal def draw_petal(tur, speed, n_sided, radius): angle = 360 / n_sided for i in range(n_sided): tur.forward(radius) tur.left(angle) # Define fuction draw_sunflower def draw_sunflower(tur, speed, n_sided, radius, num_petal): tur.speed(speed) angle_increment = 360 / num_petal for i in range(num_petal): # 先设置当前花瓣颜色,再绘制 if i % 2 == 0: tur.color("orange") # 偶数索引(0,2,4...)画橙色 else: tur.color("yellow") # 奇数索引(1,3,5...)画黄色 # 实时获取当前颜色用于调试 current_color = tur.color() print(f"Current color: {current_color[0]}, heading angle {tur.heading()} degrees") draw_petal(tur, speed, n_sided, radius) tur.left(angle_increment) # Define the main() function def main(): screen = turtle.Screen() screen.title("Sunflower Drawing") tur = turtle.Turtle() # 初始化海龟颜色为橙色,避免第一片花瓣用默认黑色 tur.color("orange") while True: speed = int(input("Enter animation speed: ")) n_sided = int(input("Enter the number of sides of a petal: ")) radius = float(input("Enter the radius of petals: ")) num_petal = int(input("Enter the number of petals: ")) draw_sunflower(tur, speed, n_sided, radius, num_petal) response = input("Draw Another Sunflower? ") if response.lower() == "yes": tur.clear() tur.penup() tur.home() tur.pendown() # 重置颜色为橙色,确保下一次绘制开头颜色正确 tur.color("orange") elif response.lower() == "no": tur.penup() tur.goto(0, -200) tur.write("Click window to exit...", align="center", font=("Courier", 12, "normal")) break else: print("Invalid command! Please try again…") screen.exitonclick() # Run the program if __name__ == "__main__": main()
关键修改说明
- 颜色设置前置:在调用
draw_petal前先设置颜色,确保当前花瓣用正确颜色绘制。 - 修正颜色循环逻辑:i为偶数时设橙色,奇数时设黄色,严格匹配橙→黄交替的要求。
- 初始化与重置颜色:在main函数中初始设置海龟颜色为橙色,且每次重新绘制时重置颜色,避免残留颜色影响。
- 调试信息实时化:每次循环都获取当前颜色,调试输出更准确。
- 优化退出逻辑:移除无效的
turtle.exitonclick(),改用screen.exitonclick()配合break,确保窗口正常响应点击退出。
内容的提问来源于stack exchange,提问作者Bongani Langa
相关产品推荐
相关产品推荐

