比较比较字符串时遇到问题@ python

having an issua comparing comparing Strings @ python

可能有更简单的方法,但我读到 Python 中没有 Switch/case。我想请用户介绍一种颜色的名称,在调用应该处理的函数后,它应该 return RGB 中的颜色代码。当第一个字母或整个单词大写时,我的 if 语句也应该接受。

发生在我​​身上的奇怪事情是,我一直在我的控制台上得到毫无意义的结果,我确定我(在某处)有一个我的赌注!

同样的输入,完全一样的词,控制台上的结果是不一样的

目前这是我的确切代码。

import pygame
import sys

from pygame.locals import *

White =(255, 0, 0)
Black = (0, 0, 0)

Red = (255, 0, 0)
Green = (0, 255, 0)
Blue = (0, 0, 255)

Yellow = (255, 255, 0)
Cyan = (0, 255, 255)
Purple = (255, 0, 255)


def set_display():
    pygame.init()
    Display = pygame.display.set_mode((400, 300))
    pygame.display.set_caption('Seda\'s drawing game')


def get_color():
    print('Please introduce one of the following colors')
    print(' \n White \n Black \n Red \n Green \n Blue \n Yellow \n Cyan \n Purple')
    color = input()
    print (color)
    if (color == 'Black') or (color == 'BLACK') or (color == 'black'):
        return Black
    elif (color == 'White') or (color == 'WHITE') or (color == 'white'):
        return White
    elif (color == 'Red') or (color == 'RED') or (color == 'red'):
        return Red
    elif (color == 'Green') or (color == 'GREEN') or (color == 'green'):
        return Green
    elif (color == 'Blue') or (color == 'BLUE') or (color == 'blue'):
        return Blue
    elif (color == 'Yellow') or (color == 'YELLOW') or (color == 'yellow'):
        return Yellow
    elif (color == 'Purple') or (color == 'PURPLE') or (color == 'purple'):
        return Purple
    elif (color == 'Cyan') or (color == 'CYAN') or (color == 'cyan'):
        return Cyan





while True:

#    set_display()
#    for event in pygame.event.get():
#       if event.type == QUIT:
#           pygame.quit()
#           sys.exit()


    final_color = get_color()
    print ( get_color())
    print (final_color)

控制台输出:

Please introduce one of the following colors

 White 
 Black 
 Red 
 Green 
 Blue 
 Yellow 
 Cyan 
 Purple
white
white
Please introduce one of the following colors

 White 
 Black 
 Red 
 Green 
 Blue 
 Yellow 
 Cyan 
 Purple
white
white
(255, 0, 0)
(255, 0, 0)
Please introduce one of the following colors

 White 
 Black 
 Red 
 Green 
 Blue 
 Yellow 
 Cyan 
 Purple
White
White
Please introduce one of the following colors

 White 
 Black 
 Red 
 Green 
 Blue 
 Yellow 
 Cyan 
 Purple
White
White
(255, 0, 0)
(255, 0, 0)
Please introduce one of the following colors

 White 
 Black 
 Red 
 Green 
 Blue 
 Yellow 
 Cyan 
 Purple
WHITE
WHITE
Please introduce one of the following colors

 White 
 Black 
 Red 
 Green 
 Blue 
 Yellow 
 Cyan 
 Purple
WHITE
WHITE
(255, 0, 0)
(255, 0, 0)
Please introduce one of the following colors

 White 
 Black 
 Red 
 Green 
 Blue 
 Yellow 
 Cyan 
 Purple

好像每2次我写点什么,如果给我返回颜色RGB代码。

我一直觉得难以理解的是为什么:

    final_color = get_color()
    print ( get_color())
    print (final_color)

没有显示完全相同的东西。

感谢大家的帮助

EDDIT1:我发现在那些印刷品上使用了额外的东西,他没有得到最后两张印刷品。还不知道为什么..

EDDIT2:我注意到我自己的错误..说白色是 (255,0,0) :D

您 return 这样做可以大大简化颜色的方式。它还修复了您的错误。

White =(255, 0, 0)
Black = (0, 0, 0)

Red = (255, 0, 0)
Green = (0, 255, 0)
Blue = (0, 0, 255)

Yellow = (255, 255, 0)
Cyan = (0, 255, 255)
Purple = (255, 0, 255)

colours = {
    'white' : White,
    'black' : Black,
    'red' : Red,
    'green' : Green,
    'blue' : Blue,
    'yellow' : Yellow,
    'cyan' : Cyan,
    'purple' : Purple
}

def get_colour():
    print('Please introduce one of the following colours')
    for key in sorted(colours):
        print(key.capitalize())
    colour = input()
    return colours[colour.lower()]

if __name__ == '__main__': #just added for preference
    while True:
        final_colour = get_colour()
        print(final_colour)

您应该熟悉 listsdictionaries 和一些基本的 Python 功能。他们让生活变得更轻松!

https://docs.python.org/3/tutorial/datastructures.html

P.S。您的 RedWhite rgb 值相同。

首先我将 get_color 函数重写为更像这样的东西

def get_color():
    print('Please introduce one of the following colors')
    print(' \n White \n Black \n Red \n Green \n Blue \n Yellow \n Cyan \n Purple')
    color = input().lower()
    print (color)
    if color == 'black': return Black
    elif color == 'white': return White
    elif color == 'red': return Red
    elif color == 'green': return Green
    elif color == 'blue': return Blue
    elif color == 'yellow': return Yellow
    elif color == 'purple': return Purple
    elif color == 'cyan': return Cyan
    else: return "Color not found!"