2016-11-02 77 views
1

我有這樣的代碼終端顏色更改在Python

import sys 
#Jordan Run Game 
class Character(): 

    def __init__(self, role): 
    print "\033[91m Warrior \033[96m Mage" 
    charRole = raw_input() 

    if charRole == "Warrior": 
     self.role = "Warrior" 
    elif charRole == "Mage": 
     self.role = "Mage" 
    elif charRole == "Rouge": 
     self.role = "Rouge" 
    elif charRole == "Healer": 
     self.role = "Healer" 
    else: 
     sys.exit() 

    print "You chose %s" % self.role 

character = Character(role = "") 

的顏色發生變化,但由於某些原因,所有的顏色變化。

例如,法師是藍色的,之後的每一個文字也是如此。

enter image description here

+1

的ANSI轉義序列只能告訴從哪裏開始* *顏色。有逃脫告訴終端回到默認顏色。在任何情況下,都有一些模塊可以自動執行此操作,因此您無需處理任何轉義序列。 – Bakuriu

+0

請參閱http://stackoverflow.com/questions/287871/print-in-terminal-with -colors-使用-蟒 –

回答

1

您需要終止顏色

'\033[0m' 

所以要

print "\033[91m Warrior \033[96m Mage\033[0m"