0

Utter newb here with a slowly but surely growing bad reputation. I want to know how my dictionary value char['bag']['Iron Ingot'], or any 'Ingot' for that matter, can be counted without showing a TypeError: 'list indices must be integers or slices, not str.' Counter doesn't seem to work, as it doesn't address the fact I am dealing with a string, not an int, that I want to count. I want to count this string and output an integer value - the number of said string I possess in my list.

char = {'name' : 'Hero',
    'lv': 3,
    'xp': 0,
    'lvnext': 83,
    'gp' : 1225,
    'bag' : ["Bronze Bastard", "Iron Ingot"],
    'stats' : {'ATT': 111,
                'DEF': 1,
                'WIS': 1,
                'hp' : 100,
                'atk' : [5,22]}}



def blacksmith():
print("The Master Smith is a giant-like abomination of a demon.")
print("Smith: Bring me ingot, and gold piece, and I will bend the metal to your liking.")
print("[1 ingot per Claw/Claymore/Helm/Gauntlets/Boots, 2 ingots per Bastard/Chain-whip, 4 ingots per Full suit/Liquid coat]")
print("[X ingot(s)] ")
item = input("Assuming you have the ingot and coin, I can make anything forged from the Titanite anvil. (Correctly type the item you want forged.)")
if item == "Iron Claws" and char['bag'].count("Iron Ingot") >= 1 and char['gp'] >= 15:
    char['bag'].append(item)
    char['bag'] - char['bag']['Iron Ingot']
    char['gp'] = char['gp'] - 15
    char['stats']['atk'][:] = [x + 1 for x in char['stats']['atk']]
    print("{} has been added to your bottomless bag!".format(item))
    chapterOne()
Joseph_C
  • 59
  • 4

1 Answers1

0

After trying your code, I found that the error occurred because of using this

char['bag']['Iron Ingot']

The error occurred because you can only access to the element inside the list by using int. The type of char['bag'] is list therefore, you couldn't do this.

To count the elements inside your list, you can simply use the Counter, to do this, you just have to import it like below

import collections

# Make a Counter object
count_object = collections.Counter(char['bag'])

# Before printing out, convert it to dict
print(dict(count_object))

You can make some changes to make your function works!