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 个回答
问题似乎出在这些代码行上:
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)
因为你之前把红色定义为假:
color1 = bool(False)
color1 = False
同样,
color2 = bool(yellow)
黄色也被定义为假。
color2 = bool(False)
color2 = False
所以当我们到达你的第一个条件判断时,
if color1 == red and color2 == blue:
计算机看到的是:
if False==False and False == False:
这会被判断为真(true)。
if True and True:
if True:
你遇到的问题部分可能是因为你使用了input()。对于Python 2.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
计算机会在程序的命名空间中查找表达式黄色的值,而在这个程序中,黄色的值是假的。
你可以尝试使用raw_input(),它不会评估输入的内容,而是简单地返回你输入的字符串值。例如:
input()
>>>yellow
这会评估为假,但
raw_input()
>>>yellow
这会评估为字符串值“黄色”。
尝试把你的颜色常量定义为字符串,而不是布尔值(bool),并且处理输入时也当作字符串来处理。所以,
yellow = "yellow"
而不是
yellow = False
你的代码有一些大问题。首先,你调用了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!')
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!')
你的问题出现在这些代码行:
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
调用之后,不过这只是个建议。
你应该把 color1
和 color2
跟字符串进行比较,而不是跟布尔值比较:
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!")