Python 2.7.8: printing color/value with out the format
我刚接触过python,学习了大约一个月。我遇到一个问题,当我运行这个代码时,它应该用红色打印出数字。第二个例子显示了它真正打印出来的内容,我被卡住了。请帮忙。
应该打印
')
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | import random class bcolors: HEADER = '\033[95m' OKBLUE ="\x1b[94m" OKGREEN ="\x1b[92m" WARNING = '\033[93m' FAIL = '\x1b[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[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 +" Not enough MP " + bcolors.ENDC) continue player.reduce_mp(cost) enemy.take_damage(magic_dmg) print(bcolors.OKBLUE +" " + 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 +" ") 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 +" ") 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中可以很好地工作,其中
1 2 | >>> print("x","y") x y |
它的意思是"打印第一个参数,然后打印分隔符(默认为空格),然后打印第二个参数"。
但在Python2中:
1 2 | >>> print("x","y") ('x', 'y') |
号
打印包含字符串的元组的表示形式。
因此,您可以使用具有许多优点的python 3,或者像这样更改代码:
1 2 3 4 | print("Enemy HP:" + bcolors.FAIL + str(enemy.get_hp()) +"/" + str(enemy.get_max_hp()) + bcolors.ENDC +" ") # note the + instead of , |
以便打印单个字符串。
它对你的其他人有用吗?
在B类颜色的Okgreen上有一个双引号"instead off simple",它可能来自这里。