【发布时间】:2020-11-14 08:26:59
【问题描述】:
col1 = input('Please input the first color: ')
col2 = input('Please input the second color: ')
while True:
if (col1 == 'red' and col2 == 'blue') or (col1 == 'blue' and col2 == 'red'):
print('purple')
elif (col1 == 'red' and col2 == 'yellow') or (col1 == 'yellow' and col2 == 'red'):
print('orange')
elif (col1 == 'blue' and col2 == 'yellow') or (col1 == 'yellow' and col2 == 'blue'):
print('green')
if col1 not in ['red', 'blue', 'yellow']:
print('Invalid')
break
我试图让用户回答“红色”、“蓝色”或“黄色”。如果他们没有在该列表中回答,程序将打印 ('invalid') 并通过再次询问用户混合哪 2 种颜色来启动循环。
【问题讨论】:
-
我希望你的输入命令在循环内,当你得到有效输入时你应该跳出循环,而不是当你得到无效输入时。
-
我当然会这样做。它还没有解决问题。只有一个接近我想要的输出。谢谢你让我知道:)
-
如果它不能解决你的问题,让作者知道,他们可能会相应地更新答案。此外,如果它足够接近,您也许能够弄清楚其余部分。
标签: python