现在已经被困在试图解决问题一个小时了

发布于 2025-01-10 12:32:52 字数 838 浏览 2 评论 0原文

此代码应该给出错误消息:“错误,请重试!”如果输入的颜色不是红色、蓝色或黄色。它给了我错误消息,同时也给了我正确的输入,但我不明白为什么。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心舞飞扬 2025-01-17 12:32:52

试试这个:

ColorOne = input('Enter the first primary color please ')
ColorTwo = input('Enter the second primary color please ')
if ColorOne not in ['red','blue','yellow']:
        print('Error please try again!')
if ColorTwo not in ['red','blue','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')

你的代码的问题在于逻辑,在现实世界(口语)中,你会说如果颜色不是红色、蓝色或黄色,那么就做一些事情,但在编程逻辑中,这不会那样工作。 !=(红色或蓝色或黄色)等于如果颜色不是红色、不是蓝色、也不是黄色,则执行某些操作。这就是利用德摩根定律。

try this:

ColorOne = input('Enter the first primary color please ')
ColorTwo = input('Enter the second primary color please ')
if ColorOne not in ['red','blue','yellow']:
        print('Error please try again!')
if ColorTwo not in ['red','blue','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')

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.

绮筵 2025-01-17 12:32:52

解决了。

ColorTwo = input('Enter the second primary color please ')
if ColorOne not in ('red', 'blue', 'yellow'):
        print('Error please try again!')
if ColorTwo not in ('red', 'blue', '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')

你需要这样检查

if ColorTwo not in ('red', 'blue', 'yellow'):

Solved it.

ColorTwo = input('Enter the second primary color please ')
if ColorOne not in ('red', 'blue', 'yellow'):
        print('Error please try again!')
if ColorTwo not in ('red', 'blue', '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')

You'll need to check like this

if ColorTwo not in ('red', 'blue', 'yellow'):
我不会写诗 2025-01-17 12:32:52

问题在于代码 ColorOne != ('red' or 'blue' or 'yellow'):。这不是检查变量是否是这些值之一的正确方法。您应该更改为 ColorOne not in ('red', 'blue', 'yellow'):

不管怎样,我做了一些改进。

ColorOne = input('Enter the first primary color please ')
ColorTwo = input('Enter the second primary color please ')
    
Colors = ('red','blue','yellow')

combination = set((ColorOne.strip().lower(), ColorTwo.strip().lower()))

if not combination.issubset(Colors):
    print('Error please try again!')
elif combination == {'blue','red'}:
    print('Purple')
elif combination == {'red','yellow'}:
    print('Orange')
elif combination == {'blue','yellow'}:
    print('Green')

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 to ColorOne not in ('red', 'blue', 'yellow'):

Regardless, I did some improvements.

ColorOne = input('Enter the first primary color please ')
ColorTwo = input('Enter the second primary color please ')
    
Colors = ('red','blue','yellow')

combination = set((ColorOne.strip().lower(), ColorTwo.strip().lower()))

if not combination.issubset(Colors):
    print('Error please try again!')
elif combination == {'blue','red'}:
    print('Purple')
elif combination == {'red','yellow'}:
    print('Orange')
elif combination == {'blue','yellow'}:
    print('Green')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文