【问题标题】:turtle triangles, and user input. python 3海龟三角形和用户输入。蟒蛇 3
【发布时间】:2015-10-18 03:57:29
【问题描述】:

在向用户询问颜色后,我试图让它绘制一个三角形的圆圈。三角形出来很好,但填充黑色。我怀疑错误出现在底部附近的“for i in range”部分中。

import turtle
def draw_triangle(side_length):#triangle def
    for x in range (3):
        turtle. left(120)
        turtle. forward(side_length)                   
    return None

def jump(distance):#jump definition
    turtle. penup()
    turtle. forward(distance)
    turtle. pendown()
    return None

def color_triangle(side_length, color):#makes the triangle color filled
    turtle. fillcolor()
    turtle. begin_fill()
    draw_triangle(size)
    turtle. end_fill()
    return None
color_a= input("choose a color for the first triangle ")
color_b= input ("choose a color for the second triangle ")
color_c= input("choose a color for the third triangle ")
back_color= input ("choose a color for the backround ")


my_colors= []

if color_a not in my_colors:
    my_colors . append (color_a)
if color_b not in my_colors:
    my_colors . append (color_b)
if color_c not in my_colors:
    my_colors . append (color_c)
color_number = 0
size= 75
move= 80
for i in range(10):
    the_color = my_colors[color_number]
    color_triangle(size, the_color)
    color_number= color_number +1
    if color_number >= len(my_colors):
        color_number=0
    jump(move)
    turtle. left (35)
turtle.bgcolor (back_color)

【问题讨论】:

  • 我没有看到任何for i in range。并请去掉.后面的空格,谢谢。
  • 向下滚动,它是move=80之后的行首

标签: python python-3.x input turtle-graphics


【解决方案1】:

当您致电turtle.fillcolor() 时,您没有使用您的颜色:

将您的 color_triangle 函数更新为:

def color_triangle(side_length, color):
    turtle.fillcolor(color)  # See here! Actually set the fill color!
    turtle.begin_fill()
    draw_triangle(size)
    turtle.end_fill()

documentation for fillcolor() 中注意,如果你不带参数调用它,它实际上会返回当前的填充颜色。在这种情况下,这不是您想要的。

【讨论】:

    猜你喜欢
    • 2019-09-04
    • 2015-06-11
    • 2017-10-01
    • 1970-01-01
    • 2015-05-18
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多