Python总是得到相同的结果

时间:2014-06-05 02:34:30

标签: python

我在下面写的程序有以下要求:

# Design a program that prompts the user to enter the names of two primary colors
# to mix.  If the user enters anything other than red, blue or yellow, the
# program should display an error message.  Otherwise the program should display 
# the name of the secondary color that results.

这是我编写的代码 - 基于我之前编写过的Java程序,显然对于Python来说是远远不够的。:

print('You will be mixing two primary colors to get a resulting color.')
print('Primary colors are blue, red and yellow \n')

red = False
blue = False
yellow = False

color1 = bool(input('Enter your first primary color: \n'))
color2 = bool(input('Enter your second primary color: \n'))

if color1 == red and color2 == blue:
        print('That makes purple!')

elif color1 == blue and color2 == red:
        print('That makes purple!')

elif color1 == yellow and color2 == red:
    print('That makes orange!')

elif color1 == red and color2 == yellow:
    print('That makes orange!')

elif color1 == blue and color2 == yellow:
    print('That makes green!')

elif color1 == yellow and color2 == blue:
    print('That makes green!')

else:
    print('You did not enter a primary color!')

无论我输入什么颜色组合,我都会得到结果“那让人发紫!”我在哪里弄错了这个程序的逻辑?此外,当我不输入绿色作为主要颜色时,我得到了这个:

Traceback (most recent call last):
File "color.py", line 19, in <module>
color1 = bool(input('Enter your first primary color: \n'))
File "<string>", line 1, in <module>
NameError: name 'green' is not defined

而不是错误消息“您没有输入原色!”

我哪里错了?

编辑:这是我的新代码,除错误外,它的工作原理。

print('You will be mixing two primary colors to get a resulting color.')
print('Primary colors are blue, red and yellow \n')

red = 1
blue = 2
yellow = 3

color1 = input('Enter your first primary color: \n')
color2 = input('Enter your second primary color: \n')

if color1 == 1 and color2 == 2:
    print('That makes purple!')

elif color1 == 2 and color2 == 1:
    print('That makes purple!')

elif color1 == 3 and color2 == 1:
print('That makes orange!')

elif color1 == 1 and color2 == 3:
print('That makes orange!')

elif color1 == 2 and color2 == 3:
print('That makes green!')

elif color1 == 3 and color2 == 2:
print('That makes green!')

else:
print('You did not enter a primary color!')

6 个答案:

答案 0 :(得分:2)

你的麻烦存在于这些方面:

color1 = bool(input('Enter your first primary color: \n'))
color2 = bool(input('Enter your second primary color: \n'))

执行此操作时,输入的任何非空值都将生成True。您只能 获取False的方法是在提示符下点击return并提交一个空字符串。您对如何处理用户输入内容的逻辑有点缺陷。你可能想要:

if color1 == 'red'

将多余的电话放到bool之后,这只是一个建议。

答案 1 :(得分:1)

color1 = bool(input('Enter your first primary color: \n'))

如果我们简化这个,我们得到

color1 = bool("red") #color1 becomes True

所以现在你的比较是评估False等于True - 完全忽略你输入的颜色。

尝试类似

的内容
RED = 'red'
BLUE = 'blue'

color1 = input('Enter your first primary color: \n').lower()
color2 = input('Enter your second primary color: \n').lower()

if color1 == RED and color2 == BLUE:
    print('That makes purple!')

答案 2 :(得分:1)

您的代码存在一些问题。首先,您致电bool(input())。只要您提供某些输入,color1和/或color2就会设置为True。

因此,您正在调用if True == False ...您的代码无法检查颜色的实际名称。相反,我建议使用普通input()来接受“字符串

以下是您编辑的代码:

print('You will be mixing two primary colors to get a resulting color.')
print('Primary colors are blue, red and yellow \n')

color1 = input('Enter your first primary color: \n')
color2 = input('Enter your second primary color: \n')

if color1 == 'red' and color2 == 'blue':
        print('That makes purple!')

elif color1 == 'blue' and color2 == 'red':
        print('That makes purple!')

elif color1 == 'yellow' and color2 == 'red':
    print('That makes orange!')

elif color1 == 'red' and color2 == 'yellow':
    print('That makes orange!')

elif color1 == 'blue' and color2 == 'yellow':
    print('That makes green!')

elif color1 == 'yellow' and color2 == 'blue':
    print('That makes green!')

else:
    print('You did not enter a primary color!')

答案 3 :(得分:0)

您应该将color1color2与非布线字符串进行比较:

color1 = input('Enter your first primary color: \n')
color2 = input('Enter your second primary color: \n')
if color1 == "red" and color2 == "blue":
    print("That makes purple!")

答案 4 :(得分:0)

问题似乎在于这些问题

red = False
blue = False
yellow = False

color1 = bool(input('Enter your first primary color: \n'))
color2 = bool(input('Enter your second primary color: \n'))

当您输入颜色时,无论颜色是什么,它都被设置为false。计算机正在发生这种情况(我将使用红色和黄色作为例子)

color1 = bool(red)

因为您之前将红色定义为False:

color1 = bool(False)
color1 = False

类似地

color2 = bool(yellow)

黄色也被定义为假

color2 = bool(False)
color2 = False

所以当我们到达你的第一个if语句时

if color1 == red and color2 == blue:

计算机看到了

if False==False and False == False:

评估为True

if True and True:
if True:

您的部分问题可能源于您使用input()。对于python2.x(我假设您正在使用)input()评估输入的内容。例如:

input("Enter Something")
>>>3
input("Enter Something Else")
>>>3.1

将分别返回整数值3和浮点值3.1。计算机会查看您输入的内容,并尽力为您提供有意义的数据。在你的情况下。

yellow = False

color1 = bool(input('Enter your first primary color: \n'))

>>>yellow

计算机在程序的命名空间中搜索表达式yellow的值,该值在此程序中为False。

它可能会帮助您使用raw_input(),它不会评估键入的内容,只返回键入的字符串值。例如:

input()
>>>yellow

将评估为False,但

raw_input()
>>>yellow

将评估字符串值&#34;黄色&#34;

尝试将颜色常量定义为字符串而不是bool并将输入作为字符串处理。所以

yellow = "yellow"

而不是

yellow = False

答案 5 :(得分:0)

我没有使用颜色变量...我只是测试变量是否= =&#39;颜色&#39;

所以我的代码是......

color1=input('Enter primary color:')
color2=input('Enter primary color:')


if color1=='red' and color2=='blue':
    print("When you mix red and blue, you get purple.")
elif color1=='red' and color2=='yellow':
    print("When you mix red and yellow, you get orange.")
elif color1=='blue' and color2=='red':
    print("When you mix blue and red, you get purple.")
elif color1=='blue' and color2=='yellow':
    print("When you mix blue and yellow, you get green.")
elif color1=='yellow' and color2=='red':
    print("When you mix yellow and red, you get orange.")
elif color1=='yellow' and color2=='blue':
    print("When you mix yellow and blue, you get green.")
else:
    print("You didn't input two primary colors.")