python - Printing certain strings different colour with termcolor.colored? -


i print strings in different colours in python. need modify code:

board_p1 = [] board_pc = []  board_size=6  x in range(board_size):     board_p1.append(["[w]"] * board_size)     board_pc.append(["[w]"] * board_size)  def print_board(board):     if board == board_p1:         print colored("\n   computers board:    ",attrs=['underline'])         row in board:             print " ".join(colored(element,"cyan") if element != "[x]" else colored(element,"red") if element != "[h]" else colored(element,"magenta") element in row)     if board == board_pc:         print colored("\n    players board:     ",attrs=['underline'])         row in board_pc:             print " ".join(colored(element,"cyan") if element != "[s]" else colored(element,"green") if element != "[x]" else colored(element,"red") if element != "[h]" else colored(element,"magenta") element in row) 

so when in list [h] printed magenta, [x] in red, etc can have output like:

enter image description here

i having difficulty with:

print " ".join(colored(element,"cyan") if element != "[s]" else colored(element,"green") if element != "[x]" else colored(element,"red") if element != "[h]" else colored(element,"magenta") element in row) 

in order print in aforementioned way.

q: how can modify/edit line of code above, if [x] seen in list it's printed red, [s] in green , [h] in magenta?

although following parse correctly due added parentheses:

print " ".join(colored(element,"cyan") if element != "[s]" else (colored(element,"green") if element != "[x]" else (colored(element,"red") if element != "[h]" else colored(element,"magenta"))) element in row) 

however due negated conditions, wasn't able tell if produce mapping described , depicted in example in question.

but going description suggest better use dictionary this:

element_colors = {'[w]': 'cyan', '[x]': 'red', '[s]': 'green', '[h]': 'magenta'}  print " ".join(colored(element, element_colors[element]) element in row) 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo