Feb-25-2017, 11:33 AM
(Feb-25-2017, 10:33 AM)Raptor88 Wrote: If there is some short code for the "dictionary mapping" approach that you can post, or a link to a simple explanation, would appreciate that.Can make one more:
color = 'red' # From eg user input if color == 'red': set_color = 'rgb(255,0,0)' elif color == 'blue': set_color = 'rgb(0,0,255)' elif color == 'green': set_color = 'rgb(0,128,0)' else: print('No rgb for that color') print(set_color) #--> rgb(255,0,0)With dictionary and
get()
method.color = 'red' # From eg user input choices = {'red': 'rgb(255,0,0)', 'blue': 'rgb(0,0,255)', 'green': 'rgb(0,128,0)'} set_color = choices.get(color, 'No rgb for that color') print(set_color) #--> rgb(255,0,0)