Python 2.7.8:打印颜色/值而不带格式



我是python的新手,学习了大约一个月。我遇到了一个问题,当我运行此代码时,它应该以红色打印出数字。第二个例子显示了它真正打印出的内容,我卡住了。请帮忙。

它应该打印('Enemy HP:', 1150/1200)
但实际上打印('Enemy HP:', 'x1b[91m1150/1200x1b[0mn')

import random
class bcolors:
HEADER = '33[95m'
OKBLUE = "x1b[94m"
OKGREEN = "x1b[92m"
WARNING = '33[93m'
FAIL = 'x1b[91m'
ENDC = '33[0m'
BOLD = '33[1m'
UNDERLINE = '33[4m'

class Person:
def __init__(self, hp, mp, atk, df, magic):
self.maxhp = hp
self.hp = hp
self.maxmp = mp
self.mp = mp
self.atkl = atk - 10
self.atkh = atk + 10
self.df = df
self.magic = magic
self.actions = ["Attack", "Magic"]
def generate_damage(self):
return random.randrange(self.atkl, self.atkh)
def generate_spell_damage(self, i):
mgl = self.magic[i]["dmg"] - 5
mgh = self.magic[i]["dmg"] + 5
return random.randrange(mgl, mgh)
def take_damage(self, dmg):
self.hp -= dmg
if self.hp < 0:
self.hp = 0
return self.hp
def get_hp(self):
return self.hp
def get_max_hp(self):
return self.maxhp
def get_mp(self):
return self.mp
def get_max_mp(self):
return self.maxmp
def reduce_mp(self, cost):
self.mp -= cost
def get_spell_name(self, i):
return self.magic[i]["name"]
def get_spell_mp_cost(self, i):
return self.magic[i]["cost"]
def choose_action(self):
i = 1
print(bcolors.OKBLUE + bcolors.BOLD + "Actions" + bcolors.ENDC)
for item in self.actions:
print(str(i) + ":", item)
i += 1
def choose_magic(self):
i = 1
print(bcolors.OKBLUE + bcolors.BOLD + "Magic" + bcolors.ENDC)
for spell in self.magic:
print(str(i) + ":", spell["name"], "(cost:", str(spell["cost"]) + ")")
i = 1

from classes.game import Person, bcolors

magic = [{"name": "Fire", "cost": 10, "dmg": 100},
{"name": "Thunder", "cost": 10, "dmg": 124},
{"name": "Blizzard", "cost": 10, "dmg": 100}]

player = Person(460, 65, 60, 34, magic)
enemy = Person(1200, 65, 45, 25, magic)
running = True
i = 0
print(bcolors.FAIL + bcolors.BOLD + "AN ENEMY ATTACKS!" + bcolors.ENDC)
while running:
print("======================")
player.choose_action()
choice = input("Choose action:")
index = int(choice) - 1
if index == 0:
dmg = player.generate_damage()
enemy.take_damage(dmg)
print("You attacked for", dmg, "points of damage.")
elif index == 1:
player.choose_magic()
magic_choice = int(input("Choose magic:")) - 1
magic_dmg = player.generate_spell_damage(magic_choice)
spell = player.get_spell_name(magic_choice)
cost = player.get_spell_mp_cost(magic_choice)
current_mp = player.get_mp()
if cost > current_mp:
print(bcolors.FAIL + "nNot enough MPn" + bcolors.ENDC)
continue
player.reduce_mp(cost)
enemy.take_damage(magic_dmg)
print(bcolors.OKBLUE + "n" + spell + " deals", str(magic_dmg), "points of damage" + bcolors.ENDC)

enemy_choice = 1
enemy_dmg = enemy.generate_damage()
player.take_damage(enemy_dmg)
print("Enemy attacks for", enemy_dmg)
print("----------------------------")
print("Enemy HP:", bcolors.FAIL + str(enemy.get_hp()) + "/" + str(enemy.get_max_hp()) + bcolors.ENDC + "n")
print("Your HP:", bcolors.OKGREEN + str(player.get_hp()) + "/" + str(player.get_max_hp()) + bcolors.ENDC)
print("Your MP:", bcolors.OKBLUE + str(player.get_mp()) + "/" + str(player.get_max_mp()) + bcolors.ENDC + "n")

if enemy.get_hp() == 0:
print(bcolors.OKGREEN + "You Win!", + bcolors.ENDC)
running = False
elif player.get_hp() == 0:
print(bcolors.FAIL + "Your enemy has defeated you!" + bcolors.ENDC)
running = False

你的代码在 Python 3 中运行良好,其中print是一个函数:

>>> print("x", "y")
x y

它的意思是"打印第一个参数,然后打印分隔符(默认为空格),然后打印第二个参数。

不过,在 Python 2 中:

>>> print("x", "y")
('x', 'y')

打印包含字符串的元组的表示形式。

因此,您可以使用具有许多优点的Python 3,也可以像这样更改代码:

print("Enemy HP:" + bcolors.FAIL + str(enemy.get_hp()) + "/" +  
str(enemy.get_max_hp()) + bcolors.ENDC + "n")
# note the + instead of ,

为了打印单个字符串。

它适用于您的其他人打印吗?

你有一个双引号",而不是在类颜色中的OKGREEN上的简单'',也许它来自这里

最新更新