gpt4 book ai didi

Python 3 类型错误 : can only concatenate tuple (not "str") to tuple

转载 作者:行者123 更新时间:2023-12-01 02:56:01 26 4
gpt4 key购买 nike

我希望为村民分配 4 个选项中的随机名称以及 4 个选项中的随机响应,但当我不断收到“TypeError: can only concatenate tuple (not “str”) to tuple”时想要打印最终的名称和响应

def villager():
responses = ("Hi", "Are you a hero?", "Are you from this village?", "There has been a dark shadow cast across the village")
npcnamechoice = ("Talion", "Ratbag", "Mildreth", "Susan")
random.choice(npcnamechoice)
npcname = npcnamechoice + ": "
print(npcname + "Hello my name is " + npcname + "would you like to talk")
random.choice(responses)
print("Press y to talk to the villager")
if input() == "y":
print(npcname + responses)
else:
print(npcname + "Goodbye")

最佳答案

您实际上并未捕获随机值。这本身只是调用函数,它没有重新分配npcnamechoice

random.choice(npcnamechoice)

因此,创建另一个变量,或内联它

 npcname = random.choice(npcnamechoice) + ": "

对响应执行相同的操作

关于Python 3 类型错误 : can only concatenate tuple (not "str") to tuple,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44218420/

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