gpt4 book ai didi

python - 类型错误:在字符串格式化期间并非所有参数都转换为彩色文本

转载 作者:行者123 更新时间:2023-12-01 01:53:31 25 4
gpt4 key购买 nike

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

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”,而是想让系统回调到“Fore.BLUE + “Warlock” + Style。 RESET_ALL”当我写术士时。我认为我的想法应该有效,但我执行错了......

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

最佳答案

这个问题不是因为 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.

关于python - 类型错误:在字符串格式化期间并非所有参数都转换为彩色文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50494737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com