turtle模块color()错误

-2 投票
2 回答
3016 浏览
提问于 2025-04-18 15:06

我正在尝试写一个程序,让用户指定线条的颜色,但我总是遇到错误。有人能解释一下为什么会这样吗?

import turtle 
wn = turtle.Screen()
alex = turtle.Turtle
sides = int(input("Enter the number of sides: "))
angle = 360/ sides
length = int(input("Enter the length of sides: "))
line_color = input("Enter the color of the lines: ")
alex.color(line_color)

fill_color = input("Enter the fill color for the polygon:" )
alex.fillcolor(fill_color)
alex.begin_fill()
for i in range(sides):
        alex.forward(lenght)
        alex.left(angle)
alex.end_fill()

2 个回答

0

你这儿有个拼写错误,朋友。 alex.forward(length)

0

这个代码应该可以运行:

import turtle    

wn = turtle.Screen()    
alex = turtle.Turtle() # need parens    
sides = int(input("Enter the number of sides: "))    
angle = 360 / sides    
length = int(input("Enter the length of sides: "))    
line_color = input("Enter the color of the lines: ") # takes input like "red" or "black" etc.. 
alex.color(line_color)    
fill_color = input("Enter the fill color for the polygon:" )    
alex.fillcolor(fill_color)    
alex.begin_fill()    
for i in range(sides):    
        alex.forward(44)  # added 44, you had an undefined variable there which is not valid    
        alex.left(angle)    
alex.end_fill()

我在这里加了一个值 44,你需要根据自己的需要添加其他值,或者把变量 length 初始化为某个值。

撰写回答