1

我正在尝试自己制作游戏,这是我的第一个游戏。在其中,我试图随着时间的推移添加功能,有一天我想出了一个为某些关键词添加颜色的想法。这就是我所拥有的:

print("The Text Based Game v0.1.0.0")
import time
import sys

time.sleep(1.5)
name = input("What is your name? ")
print("%s, are you a Warlock, Titan, or Hunter? Type 'list' if you would like a discription of each class." % (name))

playerclass = input()

if playerclass == ("list"):
  print ("\nWarlocks are powerful guardians. They hit hard but can't take in much in return. \n\nTitans are tanks. In the front of the line taking in bullets for others.\n\nHunters are the middle man. They are fast but besides that they don't have much else to bring to the table.\n")
  time.sleep(2)
  print ("Will you chose to be a Warlock, Titan, or Hunter?")

  playerclass = input()

现在,当代码询问你想成为什么职业时,我希望“术士”、“泰坦”和“猎人”这些词以不同的颜色显示。这就是我试图做的:

print("The Text Based Game v0.2.0.0")
import time
import sys
from colorama import Fore, Back, Style

print (Style.RESET_ALL)

Warlock = (Fore.BLUE + "Warlock" + Style.RESET_ALL)
Titan = (Fore.RED + "Titan" + Style.RESET_ALL)
Hunter = (Fore.GREEN + "Hunter" + Style.RESET_ALL)

time.sleep(1.5)
name = input("What is your name? ")
print ("%s, are you a " +str(Warlock)+ ", " +str(Titan)+", or" +str(Hunter)+ "? Type 'list' if you would like a discription of each class." % (name))

playerclass = input()

if playerclass == ("list"):
  print ("\nWarlocks are powerful guardians. They hit hard but can't take in much in return. \n\nTitans are tanks. In the front of the line taking in bullets for others.\n\nHunters are the middle man. They are fast but besides that they don't have much else to bring to the table.\n")
  time.sleep(2)
  print ("Will you chose to be a Warlock, Titan, or Hunter?")

  playerclass = input()

它提出一个错误,上面写着:

Traceback (most recent call last):
  File "python", line 14, in <module>
TypeError: not all arguments converted during string formatting

我不想每次都在引号内写“Fore.BLUE + "Warlock" + Style.RESET_ALL",而是希望系统在我写术士。我认为我的想法应该有效,但我执行错了......

请注意,我在 Python 3.6.1 中在线在 Repl.it 中编写了所有这些内容,这是 Repl.it 中代码的链接:https ://repl.it/@Woah_its_boobe/Error

4

2 回答 2

2

这个问题不是因为Colorama。这里的问题是%运算符的优先级高于+,因此 Python 试图在此处添加名称:

"? Type 'list' if you would like a discription of each class." % (name)

在它使用 . 组合所有这些字符串之前+。简单的解决方案是将整个字符串表达式括在括号中:

Warlock = "Warlock"
Titan = "Titan"
Hunter = "Hunter"

name = "Bode"

print(("%s, are you a " +str(Warlock)+ ", " +str(Titan)+", or" +str(Hunter)+ "? Type 'list' if you would like a discription of each class.") % name)

输出

Bode, are you a Warlock, Titan, orHunter? Type 'list' if you would like a discription of each class.

但是,如果您不手动将字符串添加在一起,并且使用format方法而不是%.

Warlock = "Warlock"
Titan = "Titan"
Hunter = "Hunter"

name = "Bode"

print("{}, are you a {}, {}. or {}?".format(name, Warlock, Titan, Hunter), end=" ")
print("Type 'list' if you would like a description of each class.")

输出

Bode, are you a Warlock, Titan. or Hunter? Type 'list' if you would like a description of each class.
于 2018-05-23T18:11:04.150 回答
1

您的问题是字符串格式不能跳过+字符串的串联。它是通过使用解决的:

Warlock = Fore.BLUE + "Warlock" + Style.RESET_ALL
Titan = Fore.RED + "Titan" + Style.RESET_ALL
Hunter = Fore.GREEN + "Hunter" + Style.RESET_ALL

time.sleep(1.5)
name = input("What is your name? ")
print ("%s, are you a " % (name) +Warlock+ ", " +Titan+", or" +Hunter+ "? Type 'list' if you would like a discription of each class.")

您必须在字符串% (name)旁边附加右侧。%s, are you a我还删除了一堆不必要的括号和东西。

于 2018-05-23T18:10:39.733 回答