可能有一种更简单的方法可以做到这一点,但我读到Python中没有Switch / case。我想问用户,引入一个颜色的名称,并在调用应该处理的函数后,它应该返回RGB中的颜色代码。当第一个字母或整个单词都是大写时,我的if语句也应该接受。
发生在我身上的奇怪的事情是,我一直在我的控制台上发现结果没有任何意义,我确定(某处)有一个mystake来自我!
输入相同的输入后,显示相同的单词,控制台上的结果不一样。
目前这是我的确切代码。
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
答案 0 :(得分:1)
通过这样做可以简化返回颜色的方式。它还修复了你的错误。
$username
您应该熟悉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)
,lists
和一些基本的Python函数。他们让生活变得更轻松!
https://docs.python.org/3/tutorial/datastructures.html
P.S。您的dictionaries
和Red
rgb值相同。
答案 1 :(得分:0)
首先我将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!"