Python主要到次要颜色

时间:2015-09-30 21:20:03

标签: python python-2.7 input

所以我有一个问题,要求我提供两种原色并告诉我他们创造的颜色,但是当我输入两种基色时,如果他们没有输入两种基色,我会一直收到错误信息。这就是我所拥有的。

red = '1'
blue = '2'
yellow = '3'

p1 = (input('Enter primary color: '))
p2 = (input('Enter primary color: '))

if p1 == '1' and p2 == '2':
    print('When you mix red and blue, you get purple')
elif p1 == '3' and p2 == '1':
    print('When you mix yellow and red, you get orange')
elif p1 == '2' and p2 == '3':
    print('When you mix blue and yellow, you get green')
else:
    print('You didnt input two primary colors.')

2 个答案:

答案 0 :(得分:0)

看来你正在使用Python 2.x.将input的匹配项替换为raw_input,您将实现目标。

答案 1 :(得分:0)

我将此视为python 3问题,因为您已对两者进行了标记。

您的问题是当您输入原色时,您输入的是“红色”,“黄色”或“蓝色”。 Python不会将这些值识别为值,因为p1被设置为“红色”而不是“1”,因此比较将不起作用。

此代码应该有效:

p1 = (input('Enter primary color (red: 1, blue: 2, yellow: 3): '))
p2 = (input('Enter primary color (red: 1, blue: 2, yellow: 3): '))

if p1 == '1' and p2 == '2':
    print('When you mix red and blue, you get purple')
elif p1 == '3' and p2 == '1':
    print('When you mix yellow and red, you get orange')
elif p1 == '2' and p2 == '3':
    print('When you mix blue and yellow, you get green')
else:
    print('You didnt input two primary colors.')

编辑:以下代码按照您的要求执行

p1 = input('Enter primary color (red, blue, yellow): ')
p2 = input('Enter primary color (red, blue, yellow): ')

red = 'red'
blue = 'blue'
yellow = 'yellow'

if p1 == red and p2 == blue:
    print('When you mix red and blue, you get purple')
elif p1 == yellow and p2 == red:
    print('When you mix yellow and red, you get orange')
elif p1 == blue and p2 == yellow:
    print('When you mix blue and yellow, you get green')
else:
    print('You didnt input two primary colors.')