【问题标题】:Some colors are not showing - turtle有些颜色没有显示 - 乌龟
【发布时间】:2020-10-14 22:11:01
【问题描述】:

我有一个使用给定参数制作形状的螺旋图代码。当我运行代码时,只有白色和红色起作用,蓝色和绿色只是呈现为白色。

print('Choose a color: ')
print('1. White')
print('2. Blue')
print('3. Green')
print('4. Red')
color1 = input('-')

那部分要求你想要的颜色

if color1 == '1':
    color = 'white'
if color1 == '2':
    color = 'blue'
if color1 == '3':
    color = 'green'
if color1 == '4':
    color = 'red'
elif color1 != '1' or '2' or '3' or '4':
    color = 'white'

该部分将输入转换为颜色

    draw = True
    t.speed(0)
    num = 0
    t.hideturtle()
    t.pencolor(color) #this part right here
    while draw == True:
        t.circle(90)
        t.rt(rotate)
        num += 1
        if num >= lines:
            draw = False
            print('Press enter to draw again!')
            continue

这是绘图循环的一部分,它将海龟颜色声明为您想要的颜色。

【问题讨论】:

    标签: python python-turtle


    【解决方案1】:

    你需要修复你的 if / else 块:

    if color1 == '1':
        color = 'white'
    elif color1 == '2':
        color = 'blue'
    elif color1 == '3':
        color = 'green'
    elif color1 == '4':
        color = 'red'
    else:
        color = 'white'
    

    您也可以使用列表来选择颜色:

    color='white'  # default
    colorlst = ['white','blue','green','red']
    keylst = ['1','2','3','4']
    if color1 in keylst:
        color=colorlst[keylst.index(color1)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-17
      • 1970-01-01
      • 2011-08-14
      • 2020-09-08
      • 2018-05-24
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      相关资源
      最近更新 更多