简体   繁体   English

Python总是得到相同的结果

[英]Python always getting same result

The program I am writing below has the follow requirements: 我在下面编写的程序具有以下要求:

# 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.

This is the code I have written - based upon a Java program I have wrote previously and evidently was way off for Python.: 这是我编写的代码-基于我之前编写的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!')

No matter what color combination I enter, I get the result "That makes purple!" 无论输入什么颜色组合,都会得到“紫色”的结果。 Where did I go wrong with the logic of this program? 该程序的逻辑哪里出了问题? Further, when I do not enter green as the primary color, I get this: 此外,当我不输入绿色作为原色时,我得到以下信息:

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

instead of the error message "You did not enter a primary color!" 而不是错误消息“您没有输入原色!”

Where am I going wrong? 我要去哪里错了?

EDIT: This is my new code and it works other than the erroring out. 编辑:这是我的新代码,除了错误输出外,它还可以工作。

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!')

Your trouble exists in these lines: 您的麻烦在于以下几行:

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

When you do this, any non-empty value entered will result in True . 执行此操作时,输入的任何非空值都将导致True The only way you'd be able to get False would be to hit return at the prompt and submit an empty string. 您能够获得False唯一方法是在提示符后按return并提交一个空字符串。 Your logic in how you want to handle what the user enters is a bit flawed. 您想要处理用户输入内容的逻辑有些缺陷。 You might want: 你可能想要:

if color1 == 'red'

After you drop the superfluous call to bool , but that's just a suggestion. 在您删除了多余的bool调用之后,但这只是一个建议。

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

If we simplify this, we get 如果我们简化一下,我们得到

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

So now your comparisons are evaluating does False equal True - completely ignoring the colors you have inputted. 因此,现在您的比较正在评估False等于True完全忽略了您输入的颜色。

Try something like 尝试类似

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!')

You have some big problems with your code. 您的代码有一些问题。 First, you call bool(input()) . 首先,您调用bool(input()) As long as you provide some input, color1 and/or color2 are set to True. 只要你提供一些输入, color1和/或color2被设置为True。

Therefore, you are calling if True == False ... Your code does nothing to check the actual names of colors. 因此,您在调用if True == False ...您的代码不执行任何操作来检查颜色的实际名称。 Instead, I suggest using plain input() to take in a ` string . 相反,我建议使用普通的input()接受` string

Here is your edited code: 这是您编辑的代码:

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!')

You should be comparing color1 and color2 to strings not booleans: 你应该比较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!")

The problem seems to lie in these lines 问题似乎出在这些方面

red = False
blue = False
yellow = False

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

when you input a color it is just being set to false, regardless of which color it is. 输入颜色时,无论颜色是哪种,它都将被设置为false。 to the computer this is happening(I'll use red and yellow as examples) 在计算机上正在发生(我将使用红色和黄色作为示例)

color1 = bool(red)

since you defined red earlier as False: 由于您之前将红色定义为False:

color1 = bool(False)
color1 = False

Similarly 相似地

color2 = bool(yellow)

yellow is also defined as False 黄色也被定义为False

color2 = bool(False)
color2 = False

So when we get to your first if statement 因此,当我们到达您的第一个if语句时

if color1 == red and color2 == blue:

the computer sees 电脑看到

if False==False and False == False:

which evaluates to True 评估为True

if True and True:
if True:

Part of your problem might be arising from your use of input(). 您使用部分input()可能会导致部分问题。 To python2.x(which I will assume you are using) input() evaluates whatever is entered. 对于python2.x(我会假设您正在使用),input()会评估输入的内容。 For example: 例如:

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

would return the integer value 3 and the float value 3.1 respectively. 将分别返回整数值3和浮点值3.1。 The computer looks at what you type in and tries its best to give you a piece of data that makes sense. 计算机会查看您键入的内容,并尽力为您提供有意义的数据。 In your case. 就你而言。

yellow = False

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

>>>yellow

the computer searches the program's namespace for the value of the expression yellow, which in this program is False. 计算机将在程序的名称空间中搜索黄色表达式的值,该表达式在该程序中为False。

It may help you to use raw_input(), which does not evaluate what is typed in and simply returns the string value of whatever is typed. 它可以帮助您使用raw_input(),它不评估所键入的内容,而只是返回所键入内容的字符串值。 For example: 例如:

input()
>>>yellow

would evaluate to False but 将评估为False,但

raw_input()
>>>yellow

would evaluate to the string value "yellow" 将评估为字符串值“ yellow”

Try defining your color constants as strings rather than bools and processing input as a string as well. 尝试将颜色常量定义为字符串而不是布尔值,并将输入也处理为字符串。 so 所以

yellow = "yellow"

rather than 而不是

yellow = False

I didn't use variables for the colors...I simply tested if the variable was=='color' 我没有为颜色使用变量...我只是测试了变量是否为=='color'

so my code was... 所以我的代码是

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM