2017-02-19 7 views
0

Я просмотрел много вопросов об этой ошибке, но они, похоже, не в состоянии помогите мне понять мою проблему с этой ошибкой, которую я получаю. В следующем коде я просто пытаюсь добавить 1 к переменной. Эта переменная никогда не является частью ввода от пользователя, поэтому у меня возникают проблемы с ней.Получение этого TypeError: Невозможно преобразовать объект 'int' в str неявно, когда я не пытаюсь преобразовать значение int

Вот отслеживающий:

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 

Я собираюсь поставить модуль, что это является частью так что вы можете видеть, но как я уже сказал, ни в одной точке является переменная часть healthPot от входа от пользователя, и я не знаю, почему он думает, я хочу, чтобы изменить значение INT переменной на значение ул:

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 

Я знаю, что у меня, вероятно, есть много кода, который можно было бы очистить, но я все еще участвую, и я использую эту программу больше, чтобы видеть, что я могу и чего не могу сделать, и я думаю, что это весело.

Благодарим вас за помощь ребятам!

ответ

0

Сообщение об ошибке

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

не жалуется, что он думает, что вы хотите конвертировать healthPot в строку.

healthPot является уже строка, Python жалуется, что вы пытаетесь объединить целое с ним ..., который должен был бы неявно преобразовать целое число в строку, чтобы сделать это.

Я не знаю, как healthPot в конечном итоге в виде строки, но это, безусловно, так, как добавление строки

print(type(healthPot)) 

непосредственно перед healthPot += 1 линия подтвердит.

+0

Хм .. выглядит как больше кода копать для меня тогда. –