I use Anaconda 3 > Python 3 > Spyder 3. I am currently working on a text based survival game. I am using a dictionary for my inventory. I have figured out how to add and remove items from my inventory, but I have some def blablabla():
used in my game market for buying and selling items. The problem is, I'm not sure if I'm supposed to use def blablabla():
for my market. Most importantly though, when in the def BuyThisStuff():
I use a line of code that is supposed to check if I have enough currency to purchase that thing. That is where I get an error saying: TypeError: '>' not supported between instances of 'str' and 'int'
here is my inventory code:
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)
To add an item to my inventory I use:
inventory['Rock'] = '3'
To remove:
del inventory['Rock']
Here is the error code:
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!")
(Error is in line 3)
if inventory['coins':] > 299:
As shown in the inventory code, you start with 750 coins. Now I need def MarketBuyFishingRod ():
to check if the player has over 299 coins in their inventory. Unfortunately this raises an error I do not know how to fix. Please Reply and help me, I would really appreciate your help. If you need any extra info reply and I will answer.
ANSWER: For dictionary inventories, such as inventory = {'Coins' = '42', 'Loaf Of Bread' = '2',}
, make sure to change the values to ints: 'Coins' = 42, 'Loaf Of Bread' = 2
. If you are going for looks and want your inventory to look like this: 42 Gold Coins 1 Rope 6 Torches
, and not like this: {'Coins' = 42, 'Loaf Of Bread' = 2}
, add this code into your inventory(recommend putting this into an inventory def():) for k,v in inventory.items(): print("{} {}".format(v, k))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.