[英]Python always getting same result
我在下面编写的程序具有以下要求:
# 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!')
您的麻烦在于以下几行:
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 = 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!')
您的代码有一些大问题。 首先,您调用bool(input())
。 只要你提供一些输入, color1
和/或color2
被设置为True。
因此,您在调用if True == False
...您的代码不执行任何操作来检查颜色的实际名称。 相反,我建议使用普通的input()
接受` string 。
这是您编辑的代码:
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
和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!")
问题似乎出在这些方面
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)
黄色也被定义为False
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
计算机将在程序的名称空间中搜索黄色表达式的值,该表达式在该程序中为False。
它可以帮助您使用raw_input(),它不评估所键入的内容,而只是返回所键入内容的字符串值。 例如:
input()
>>>yellow
将评估为False,但
raw_input()
>>>yellow
将评估为字符串值“ yellow”
尝试将颜色常量定义为字符串而不是布尔值,并将输入也处理为字符串。 所以
yellow = "yellow"
而不是
yellow = False
我没有为颜色使用变量...我只是测试了变量是否为=='color'
所以我的代码是
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.")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.