获取此TypeError:当我不尝试转换int值时,无法隐式地将'int'对象转换为str

问题描述 投票:0回答:1

我已经查看了很多关于此错误的问题,但他们似乎无法帮助我理解我遇到此错误的问题。在下面的代码中,我只是尝试将1添加到变量中。变量永远不会是用户输入的一部分,这就是我遇到麻烦的原因。

这是追溯:

Traceback (most recent call last):
  File "I:/Python Programs/troll game 2.py", line 966, in <module>
    main()
  File "I:/Python Programs/troll game 2.py", line 35, in main
    trolls, playersGold = getmainFight(playersHP, playersMP, trollsHP, mobLevel, trolls, playersGold, dmgBonus, classDMGbonus, weaponBreak, trollBonus, healthPot, manaPot, playerClass, pMaxHP, pMaxMP, playerLevel)
  File "I:/Python Programs/troll game 2.py", line 220, in getmainFight
    playersHP, playersGold, playersMP, dmgBonus, healthPot, manaPot, playerXP, trolls = gettrollDeath(classDMGbonus, playerXP, mobLevel, trolls, playersHP, playersGold, playersMP, playerClass, healthPot, manaPot, dmgBonus, pMaxHP, pMaxMP, playerLevel)
  File "I:/Python Programs/troll game 2.py", line 347, in gettrollDeath
    healthPot, dmgBonus, playersHP, playersGold, manaPot, pMaxHP = getplayerRewards(healthPot, dmgBonus, playersHP, playersGold, playerClass, manaPot, pMaxMP)
  File "I:/Python Programs/troll game 2.py", line 378, in getplayerRewards
    healthPot += 1
TypeError: Can't convert 'int' object to str implicitly

我将把这个部分放在模块中,这样你就可以看到,但就像我说的那样,健康点变量是用户输入的一部分,我不知道为什么它认为我想改变变量的int值为str值:

def getplayerRewards(healthPot, dmgBonus, playersHP, playersGold, playerClass, manaPot, pMaxHP):
    loot = random.randint(1, 100)
    if loot >= 96:
            pMaxHP += 5
            playersHP = pMaxHP
            print("A cleric passes by and buffs you for 5HP's, you now have", playersHP, "HP's!")                

    elif loot >= 76 and loot <= 95:
            dmgBonus += 2
            if playerClass == "Warrior":
                    print("You search around and find a Wet Stone to sharpet your Sword!")

            if playerClass == "Cleric":
                    print("You search around and find a Magic Wand!")

            if playerClass == "Mage":
                    print("You search around and find a Magic Wand!")

            if playerClass == "Scout":
                    print("You search around and find some Poison for your weapon!")

    elif loot >= 46 and loot <= 75:
            healthPot += 1
            print("You search around and find a Health Potion.")

    elif loot >= 16 and loot <= 45:
            manaPot += 1
            print("You search around and find a Mana Potion.")

    else:
            goldReward = random.randint(3, 10)
            playersGold += goldReward
            print("You search around and find", goldReward, "gold pieces!")

    return healthPot, dmgBonus, playersHP, playersGold, manaPot, pMaxHP

当用户选择他的角色类时,首先设置healthPot变量,例如:

if playerClass == "Scout":
            playerLevel = 1
            pMaxHP += 10
            playersHP = pMaxHP
            pMaxMP += 0
            playersMP = pMaxMP
            trollsHP += 0
            mobLevel += 0
            trolls += 0
            playersGold += 30
            dmgBonus += 0
            classDMGbonus += 3
            weaponBreak += 0
            trollBonus += 0
            healthPot += 2
            manaPot += 0

我知道我可能有很多可以清理的代码,但我还在学习,我正在使用这个程序来查看我能做什么和不能做什么,我认为这很有趣。

谢谢你的帮助!

python python-3.x type-conversion python-3.5
1个回答
0
投票

错误消息

TypeError: Can't convert 'int' object to str implicitly

并不抱怨它认为你想将healthPot转换为字符串。

healthPot已经是一个字符串,并且Python抱怨你正在尝试连接一个整数...它必须隐式地将整数转换为字符串才能这样做。

我不知道healthPot是如何最终成为一个字符串,但这肯定是这样的,因为添加线

print(type(healthPot))

healthPot += 1线确认之前。

© www.soinside.com 2019 - 2024. All rights reserved.