一些颜色没有显示-乌龟

问题描述

我有一个呼吸描记器代码,可以使用给定的参数制作形状。当我运行代码时,只有白色和红色起作用,而蓝色和绿色只是白色。

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

这是绘制循环的一部分,该循环将乌龟颜色声明为所需的颜色。

解决方法

您需要修复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)]