我无法修复这个 TypeError

时间:2021-01-11 23:23:45

标签: python python-3.x m dd repl.it

错误信息:

`Traceback (most recent call last):
  File "main.py"  line 23, in <module>
    if gold>="4":
TypeError: '>=" not supported between instances of 'int' and 'str'`

error message screenshot console screenshot

我的代码:

代码:

import random
hp=34
maxhp=34
strenth=5
weapon='fist'
gold=0
monsters=["goblin","Orc","hobgoblin","dragon","Troll","tarasque"]
while hp>=1:
  print('\ngold:',gold)
  print('\nhp:',hp)
  print('\nmaxhp:',maxhp)
  print('\nstrenth:',strenth)
  print('\nweapon:',weapon)
  print("\n\n[1]BATTLE!!!\n[2]shop")
  answer=input()
  if answer=='1':
    random.choice(monsters)
  elif answer=="2":
    print('what would nyou like to purchase?\n\n[1]small heal                       5 gold\n[2]moderate heal                   20 gold\n[3]large heal                     100 gold\n[4]small strenth boost             50 gold\n[5]moderate strenth boost         200 gold\n[6]large strenth boost           1000 gold\n[7]small maxhp boost              500 gold\n[8]moderate maxhp boost          2000 gold\n[9]large maxhp boost            10000 gold\n[10]sacrafice 20 hp for 5 gold     20   hp\n\n\nwhatis ur choice')
    print('gold:',gold)
    shop=input()
    if shop=='1':
      if gold>="4":
        gold-=5

    elif shop=='2':
      if gold>="19":
        gold-=20

    elif shop=='3':
      if gold >= "99":
        gold-=100

    elif shop=='4':
      if gold >= "49":
        gold-=50

    elif shop == '5':
      if gold>="199":
        gold-=200

    elif shop == '6':
      if gold>="999":
        gold-=1000

    elif shop == '7':
      if gold>="499":
        gold-=500

    elif shop == '8':
      if gold>="1999":
        gold-=2000

    elif shop == '9':
      if gold>="9999":
        gold-=10000

    elif shop == '10':
      hp-=20
      gold+=5
      print("you sacraficed your health to gain money. not exacly a wise move.")
      input()
    else:
      print('what? thats not a legal input\nnow get out!')
      input()
while hp>=maxhp:
  hp-=1
while hp<=1:
  print("G A M E\n   O V E R")

我对 python 比较陌生,我研究了一些解决方案不起作用的网站。为此,我一直在使用 repl.it。

2 个答案:

答案 0 :(得分:1)

错误是由于您正在比较整数和字符串两种数据类型。

gold=0 # integer value
if gold>="4": # string, just write it as 4
        gold-=5

我想这对你有帮助。

答案 1 :(得分:1)

Python 中所有输入都是字符串,如果你只需要将它们用作整数,只需立即转换即可。我继续改进你的代码。附注。为什么菜鸟从不使用空格

import random
hp = 34
maxhp = 34
strenth = 5
weapon = "fist"
gold = 1000
monsters = ["goblin", "Orc", "hobgoblin", "dragon", "Troll", "tarasque"]

 shop_choices = {
        1: 5, 2: 20, 3: 50,
        4: 100, 5: 200, 6: 500,
        7: 1000, 8: 2000, 9: 10000
    }

def spend_gold(gold, amount):
        if gold >= amount:
            gold -= amount
        return gold

while hp >= 1:
    print('\ngold:', gold)
    print('\nhp:', hp)
    print('\nmaxhp:', maxhp)
    print('\nstrenth:', strenth)
    print('\nweapon:', weapon)
    print("\n\n[1]BATTLE!!!\n[2]shop")
    answer = int(input()

    if answer == 1:
        random.choice(monsters)
    elif answer == 2:
        print("what would nyou like to purchase?\n\n[1]small heal                       5 gold\n[2]moderate heal                   20 gold\n[3]large heal                     100 gold\n[4]small strenth boost             50 gold\n[5]moderate strenth boost         200 gold\n[6]large strenth boost           1000 gold\n[7]small maxhp boost              500 gold\n[8]moderate maxhp boost          2000 gold\n[9]large maxhp boost            10000 gold\n[10]sacrafice 20 hp for 5 gold     20   hp\n\n\nwhatis ur choice")
        print("gold:", gold)
        shop = int(input())
        if shop in shop_choices:
            gold = spend_gold(gold, shop_choices[shop])
        elif shop == 10:
            hp -= 20
            gold += 5
            print("you sacraficed your health to gain money. not exacly a wise move.")
            input()
        else:
            print('what? thats not a legal input\nnow get out!')
            input()
while hp >= maxhp:
    hp -= 1
while hp <= 1:
    print("G A M E\n   O V E R")