现在已经被困在试图解决问题一个小时了
此代码应该给出错误消息:“错误,请重试!”如果输入的颜色不是红色、蓝色或黄色。它给了我错误消息,同时也给了我正确的输入,但我不明白为什么。
ColorOne = input('Enter the first primary color please ')
ColorTwo = input('Enter the second primary color please ')
if ColorOne != ('red' or 'blue' or 'yellow'):
print('Error please try again!')
if ColorTwo != ('red' or 'blue' or 'yellow'):
print('Error please try again!')
if ColorOne == ('red') and ColorTwo == ('blue'):
print('Purple')
if ColorOne == ('red') and ColorTwo == ('yellow'):
print('Orange')
if ColorOne == ('blue') and ColorTwo == ('yellow'):
print('Green')
if ColorTwo == ('red') and ColorOne == ('blue'):
print('Purple')
if ColorTwo == ('red') and ColorOne == ('yellow'):
print('Orange')
if ColorTwo == ('blue') and ColorOne == ('yellow'):
print('Green')
This code is supposed to give an error message, "Error please try again!" If the colors entered are not red, blue, or yellow. It's giving me the error message while also giving me the correct input and I cannot figure out why.
ColorOne = input('Enter the first primary color please ')
ColorTwo = input('Enter the second primary color please ')
if ColorOne != ('red' or 'blue' or 'yellow'):
print('Error please try again!')
if ColorTwo != ('red' or 'blue' or 'yellow'):
print('Error please try again!')
if ColorOne == ('red') and ColorTwo == ('blue'):
print('Purple')
if ColorOne == ('red') and ColorTwo == ('yellow'):
print('Orange')
if ColorOne == ('blue') and ColorTwo == ('yellow'):
print('Green')
if ColorTwo == ('red') and ColorOne == ('blue'):
print('Purple')
if ColorTwo == ('red') and ColorOne == ('yellow'):
print('Orange')
if ColorTwo == ('blue') and ColorOne == ('yellow'):
print('Green')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
你的代码的问题在于逻辑,在现实世界(口语)中,你会说如果颜色不是红色、蓝色或黄色,那么就做一些事情,但在编程逻辑中,这不会那样工作。 !=(红色或蓝色或黄色)等于如果颜色不是红色、不是蓝色、也不是黄色,则执行某些操作。这就是利用德摩根定律。
try this:
The problem with your code is with the logic, in real world (spoken language) you would say if the color is NOT red or blue or yellow then do something, but in programming logic this doesn't work like that. the != (red or blue or yellow) is equal to if the color is not red and not blue and not yellow then do something. And that's using de morgan laws.
解决了。
你需要这样检查
Solved it.
You'll need to check like this
问题在于代码
ColorOne != ('red' or 'blue' or 'yellow'):
。这不是检查变量是否是这些值之一的正确方法。您应该更改为ColorOne not in ('red', 'blue', 'yellow'):
不管怎样,我做了一些改进。
The problem is the code
ColorOne != ('red' or 'blue' or 'yellow'):
. It is not the correct way to check if a variable is one of those values. You should change toColorOne not in ('red', 'blue', 'yellow'):
Regardless, I did some improvements.