How to change the color of items in a list in Python?

Asked

Viewed 243 times

-1

I am beginning to learn Python and have little knowledge in this area. In order to improve my knowledge, I developed an algorithm for a game of dice that I own. It works perfectly. However, I would like to implement some colors in the texts to make it more practical.

Follow the code below.

# Simulação de batalha Massive Darkness

import random 

#dados
yellow_dice=[0,1,1,1,1,2]
red_dice=[0,1,1,2,2,3]
blue_dice=[0,0,1,1,1,2]
green_dice=[0,0,1,2,2,3]

#dados de ataque
dados_ataque_1=int(input('Quantos dados de ataque amarelo serão jogados? '))
print ()
dados_ataque_2=int(input('Quantos dados de ataque vermelho serão jogados? '))
ataque_yellow=[]
ataque_red=[]
bonus_ataque=[]
print ()

#dados de defesa
dados_defesa_1=int(input('Quantos dados de defesa azul serão jogados? '))
print ()
dados_defesa_2=int(input('Quantos dados de ataque verde serão jogados? '))
def_blue=[]
def_green=[]
bonus_def=[]


if (dados_ataque_1==1):
  yellow_1=(random.choice(yellow_dice))
  ataque_yellow.append(yellow_1)
elif (dados_ataque_1==2):
  yellow_1=(random.choice(yellow_dice))
  yellow_2=(random.choice(yellow_dice))
  ataque_yellow.append(yellow_1)
  ataque_yellow.append(yellow_2)
elif (dados_ataque_1==3):
  yellow_1=(random.choice(yellow_dice))
  yellow_2=(random.choice(yellow_dice))
  yellow_3=(random.choice(yellow_dice))
  ataque_yellow.append(yellow_1)
  ataque_yellow.append(yellow_2)
  ataque_yellow.append(yellow_3)
if (dados_ataque_2==1):
  red_1=(random.choice(red_dice))
  ataque_red.append(red_1)
elif (dados_ataque_2==2):
  red_1=(random.choice(red_dice))
  red_2=(random.choice(red_dice))
  ataque_red.append(red_1)
  ataque_red.append(red_2)
elif (dados_ataque_2==3):
  red_1=(random.choice(red_dice))
  red_2=(random.choice(red_dice))
  red_3=(random.choice(red_dice))
  ataque_red.append(red_1)
  ataque_red.append(red_2)
  ataque_red.append(red_3)

ataque_total=(sum(ataque_yellow) + sum(ataque_red))

for a in (ataque_yellow):
  if (a==2):
    bonus_ataque.append('☼')

for b in (ataque_red):
  if (b==2):
    bonus_ataque.append('☼')

for c in (ataque_red):
  if (c==3):
    bonus_ataque.append('♦')

print()
print()
print ('Os dados de ataque amarelo foram:', ataque_yellow)
print ('Os dados de ataque vermelho foram:', ataque_red)
print ('Os bônus de ataque são:' ,bonus_ataque)
print ('O ataque total é:', ataque_total)

if (dados_defesa_1==1):
  blue_1=(random.choice(blue_dice))
  def_blue.append(blue_1)
elif (dados_defesa_1==2):
  blue_1=(random.choice(blue_dice))
  blue_2=(random.choice(blue_dice))
  def_blue.append(blue_1)
  def_blue.append(blue_2)
elif (dados_defesa_1==3):
  blue_1=(random.choice(blue_dice))
  blue_2=(random.choice(blue_dice))
  blue_3=(random.choice(blue_dice))
  def_blue.append(blue_1)
  def_blue.append(blue_2)
  def_blue.append(blue_3)
if (dados_defesa_2==1):
  green_1=(random.choice(green_dice))
  def_green.append(green_1)
elif (dados_defesa_2==2):
  green_1=(random.choice(green_dice))
  green_2=(random.choice(green_dice))
  def_green.append(green_1)
  def_green.append(green_2)
elif (dados_defesa_2==3):
  green_1=(random.choice(green_dice))
  green_2=(random.choice(green_dice))
  green_3=(random.choice(green_dice))
  def_green.append(green_1)
  def_green.append(green_2)
  def_green.append(green_3)

def_total=(sum(def_blue) + sum(def_green))

for d in (def_blue):
  if (d==2):
    bonus_def.append('☼')

for e in (def_green):
  if (e==2):
    bonus_def.append('☼')

for f in (def_green):
  if (f==3):
    bonus_def.append('♦')

print()
print()

print ('Os dados de defesa azul foram:', def_blue)
print ('Os dados de defesa verde foram:', def_green)
print ('Os bônus de defesa são:' ,bonus_def)
print ('A defesa total é:', def_total)

print()
print()

resultado=(ataque_total-def_total)

if (ataque_total>def_total):
  print ('O atacante causou', resultado, 'de dano')
else:
  print('O defensor não sofreu dano')

I would like the values of the vectors/lists to stay with their respective colors.

ataque_yellow=[]
ataque_red=[]
def_blue=[]
def_green=[]

1 answer

1

You can color the prints with the colors you want, the color codes are these:

Azul = '\033[94m'
Verde = '\033[92m'
Amarelo = '\033[93m'
Vermelho = '\033[91m'
Fim = '\033[0m'

In the prints:

print ('Os dados de ataque amarelo foram: \033[93m' + ataque_yellow + '\033[0m')

To make it easier, you can use this class I found in Stack Overflow, which is where I got this answer from.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.