TypeError:只能将str(而不是“ int”)连接到str是什么意思?

时间:2018-08-27 19:11:19

标签: python python-3.x

def myFunc(King):
    print("The characters name is " + King.name + " and age is " + King.age)

如何将int链接到字符串?

2 个答案:

答案 0 :(得分:4)

这恰好表示错误消息所说的内容:display是一个字符串。您不能在其中添加整数("The characters name is " + King.name + " and age is "

幸运的是,您可以使用多种解决方法:

  1. King.age
  2. print("The characters name is", King.name, "and age is", King.age)(需要Python> = 3.6
  3. print(f"The characters name is {King.name} and age is {King.age}")
  4. print("The characters name is {King.name} and age is {King.age}".format(King=King))

...

答案 1 :(得分:0)

def myFunc(King):
    print("The characters name is " + str(King.name) + " and age is " + str(King.age))