【问题标题】:Python Inventory Dictionary Text GamePython 库存字典文字游戏
【发布时间】:2020-05-13 10:21:53
【问题描述】:

我使用 Anaconda 3 > Python 3 > Spyder 3。我目前正在开发基于文本的生存游戏。我正在为我的库存使用字典。我已经知道如何从我的库存中添加和删除物品,但我有一些 def blablabla(): 在我的游戏市场中用于购买和出售物品。问题是,我不确定我是否应该为我的市场使用def blablabla():。最重要的是,在def BuyThisStuff(): 中,我使用了一行代码来检查我是否有足够的货币来购买该东西。这就是我收到错误消息的地方:TypeError: '>' not supported between instances of 'str' and 'int' 这是我的库存代码:

inventory = {'coins':'750',
"Loaf of Bread": "2",
"Bottle of Water": "3",}
def displayInventory(inventory):
    print("Inventory:")
    item_total = (inventory.values())
    item_total = sum(map(int, item_total))
    for k, v in inventory.items():
        print(v + '   ' + k)
    print("Total number of items: " + str(item_total))
displayInventory(inventory)

要将物品添加到我使用的库存中:

inventory['Rock'] = '3'

删除:

del inventory['Rock']

这是错误代码:

def MarketBuyFishingRod ():
    global coins
    if inventory['coins':] > 299:
        inventory['coins'] = inventory['coins'] - 300
        print ("You have bought a fishing rod for 300 coins!")
        time.sleep(1)
        print ("Now in your inventory you have:")
        #inventory = inventory + ["Loaf of Bread", ]
        inventory['Fishing Rod'] = '1'
        print (inventory)
        InvFishingRod = True
     else:
        print ("You need to have at least 300 coins to buy a Fishing Rod!")

(错误在第 3 行)

if inventory['coins':] > 299:

如库存代码所示,您从 750 个硬币开始。现在我需要def MarketBuyFishingRod (): 来检查玩家的库存中是否有超过 299 个硬币。不幸的是,这引发了一个我不知道如何解决的错误。请回复并帮助我,我非常感谢您的帮助。 如果您需要任何额外的信息回复,我会回答。

【问题讨论】:

  • 你真的应该阅读一下minimal reproducible example。所有这些故事和代码都与解决简单的TypeError 无关...无论如何,错误有什么不清楚的地方? inventory 的值是字符串('750''2' 等),299 是一个整数。要么做int(inventory["coins"]),要么简单地将值保存为整数:inventory = {"coins": 750}
  • 获取unsupported operand type(s) for -: 'str' and 'int'error。更改库存字符串 intergals 没有帮助:int(inventory = {'coins':'750',})。目标:def FishingRod(): 检查库存,如果玩家有超过 299 个“硬币”。这是我的完整代码:inventory = {'coins':'750',} def displayInventory(inventory): print (inventory) displayInventory(inventory) def FishingRod (): if inventory["coins"] > "299": inventory['coins'] = inventory['coins'] - 300 inventory['Fishing Rod'] = '1' print (inventory) FishingRod ()
  • 完整代码:inventory = {'coins':'750',} def displayInventory(inventory): print(inventory) displayInventory(inventory) def FishingRod(): if inventory["coins"] > "299": inventory['coins'] = inventory['coins'] - 300 inventory['Fishing Rod'] = '1' print (inventory) FishingRod ()
  • 正如我所说,将值更改为整数。为什么将硬币数量保存为字符串?改为inventory = {'coins': 750}
  • 我将库存字符串更改为整数,谢谢,它确实解决了我的问题,但我不得不删除代码:for k, v in inventory.items(): print(v + ' ' + k),它将像这样打印出我的库存:12 Arrows 42 Gold Coins 但现在它会像这样打印出我的库存:{'coins': 750}。如果我恢复必须删除的代码,我会收到此错误:print(v + ' ' + k) TypeError: unsupported operand type(s) for +: 'int' and 'str'

标签: python python-3.x


【解决方案1】:

回答:对于字典清单,例如inventory = {'Coins' = '42', 'Loaf Of Bread' = '2',},请确保将值更改为整数:'Coins' = 42, 'Loaf Of Bread' = 2。如果您要寻找外观并希望您的库存看起来像这样:42 Gold Coins 1 Rope 6 Torches,而不是像这样:{'Coins' = 42, 'Loaf Of Bread' = 2},请将此代码添加到您的库存中(建议将其放入库存 def():) for k,v in inventory.items(): print("{} {}".format(v, k))

【讨论】:

    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    相关资源
    最近更新 更多