首页 > 解决方案 > 带有打印功能的 Python 3 中的 TypeError

问题描述

所以我一直在关注这段代码并将其放入,一切似乎都进展顺利,直到我打印最终字符串的最后一行出现 TypeError。我的错误在哪里?

kingName = input("Yo King! Please type in your name at the prompt")
numJewels = input("Hey " + kingName + ", how many jewels are there?")
numJewels = int(numJewels)
costOfEachJewel = input("Yo " + kingName + ", how much does each jewel cost?")
costOfEachJewel = int(costOfEachJewel)
print (costOfEachJewel * numJewels)
dudeNames = ["Athos", "Pothos", "Aramis"]
dudeAges = [55,34, 67]
dudeNames.insert(0, "D'Artagnan")
print (dudeNames)
dudeAges.append(16)
print (dudeAges)
tempVariable = dudeNames.pop(0)
tempVariable
dudeNames.append(tempVariable)
print (dudeNames)
print (dudeAges)
print ("Total number of dudes: " + str(len(dudeNames)))
dudeToKill = input("Yo " + kingName + "please enter the # of the dude to kill")
print ("zapping all history of " + str(len(dudeNames[dudeToKill-1])))

标签: python-3.xstringtypeerror

解决方案


dudeToKill必须是一个,在执行操作之前int转换为inputintdudeToKill - 1

dudeToKill = int(input("Yo " + kingName + "please enter the # of the dude to kill"))

推荐阅读