Python 3 TypeError:只能将元组(不是" str")连接到元组

时间:2017-05-27 15:08:44

标签: python

我想让一个村民从4个选项中分配一个随机名称以及4个选项中的随机响应但我一直得到" TypeError:只能连接元组(不是" str&# 34;)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")

1 个答案:

答案 0 :(得分:2)

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

random.choice(npcnamechoice)

所以,制作另一个变量,或者内联它

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

对回复做同样的事情