【问题标题】:TypeError in Python 3 with the print function带有打印功能的 Python 3 中的 TypeError
【发布时间】:2020-04-19 20:47:30
【问题描述】:

所以我一直在关注这段代码并将其放入,一切似乎都很顺利,直到我打印最终字符串的最后一行出现 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])))

【问题讨论】:

  • 发布完整的堆栈跟踪
  • dudeToKill 是一个字符串,你希望dudeToKill - 1 做什么?

标签: python-3.x string typeerror


【解决方案1】:

dudeToKill 必须是int,在执行dudeToKill - 1 操作之前将input 转换为int

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

【讨论】:

    【解决方案2】:
    kingName = input("Yo King! Please type in your name at the prompt")
    numJewels = int(input("Hey " + kingName + ", how many jewels are there?"))
    costOfEachJewel = int(input("Yo " + kingName + ", how much does each jewel cost?"))
    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 = int(input("Yo " + kingName + "please enter the # of the dude to kill"))
    print("zapping all history of " + str(len(dudeNames[dudeToKill-1])))
    

    希望这会有所帮助

    【讨论】:

    • 总是试图指出 OP 的错误。很明显,他在一行中犯了错误。如果你不说清楚,他可能不会意识到这一点。其他 cmets 也在这样做!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    相关资源
    最近更新 更多