我不确定为什么,但是当循环结束后我要求将其打印时,我的代码不会显示所选颜色的变量内容。程序运行时,所有输出为空白行,不输出任何文本。
如果有人可以提供对您有帮助的指针,谢谢。
def inputs(): firstColour = "" #Variables
表示由for loop secondColour = "" thirdColour = "" patchSize = "" #States the number of patches in the grid
for i in range(3):
patchColour = input ("Please choose a colour: r, g, b, m, c, o: ").lower()
if patchColour == "r":
colour = "red"
print (colour)
elif patchColour == "g":
colour = "green"
print (colour)
elif patchColour == "b":
colour = "blue"
print (colour)
elif patchColour == "m":
colour = "magneta"
print (colour)
elif patchColour == "c":
colour = "cyan"
print (colour)
elif patchColour == "o":
colour = "orange"
else:
print("No valid input has been entered")
break
if i == 0:
colour == firstColour
elif i == 1:
colour == secondColour
elif i == 2:
colour == thirdColour
print(firstColour, secondColour, thirdColour)
答案 0 :(得分:1)
本节:
if i == 0:
colour == firstColour
elif i == 1:
colour == secondColour
elif i == 2:
colour == thirdColour
应该是:
if i == 0:
firstColour = colour
elif i == 1:
secondColour = colour
elif i == 2:
thirdColour = colour
“ ==”用于比较项目是否相同,“ =”用于为变量分配值。另外,颜色和firstColour等...是错误的处理方法。当您稍后先打印出颜色时,您需要先为其分配一个值