Search code examples
pythonlistdictionarytypeerrorcallable

Including an if statement for a merged dictionary causes 'TypeError: 'int' object is not callable


First time poster here and Python newbie.

To get to grips with the basics of Python I've started by reading Automate The Boring Stuff by Al Sweigart, and there was one mini project that I thought I'd try my hand in, which was the "Fantasy Inventory" project. I managed to figure out how it works with some trial and error (and a lot of Googling), but here's the final code:

stuff = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}

def displayInventory(inventory):
    total_items = 0
    for item, quantity in inventory.items():
        print(str(quantity) + ' ' + item)
        total_items += quantity
    print("Total number of items: " + str(total_items))

displayInventory(stuff)

I decided to try and include a "precious minerals" dictionary so that it would add a little extra flavour to the text, including an if and elif statement if the preciousMineral total was 0 or more than 0. The code now looks like this:

stuff = {'arrows': 41, 'sword': 1, 'dagger': 2, 'torch': 1}
preciousMinerals = {'rubies': 0, 'emeralds': 0, 'sapphires': 0}
stuffAndMinerals = stuff|preciousMinerals

def displayInventory(inventory):
    total_items = 0
    for item, quantity in inventory.items():
        print(str(quantity) + ' ' + item)
        total_items += quantity
    print('You have a total of ' + str(total_items) + ' items in your bag.')
    if str(quantity(preciousMinerals)) == 0:
        print('You have no precious minerals.')
    elif str(quantity(preciousMinerals)) > 0:
        print('You have some precious minerals in your bag.')
        print('You have: ' + str(quantity(preciousMinerals[0]) + ', ' +
                             str(quantity(preciousMinerals[1]) + ', ' +
                             str(quantity(preciousMinerals[2]) + '.'))))
displayInventory(stuffAndMinerals)

Before adding the precious minerals, the code ran smoothly with no errors. However, I now get a 'TypeError: 'int' object is not callable' error on line:

if str(quantity(preciousMinerals)) == 0:

Any help would be greatly appreciated! Thank you very much.


Solution

  • You are trying to call quantity which is an int with preciousMinerals here:

    if str(quantity(preciousMinerals)) == 0: I'd advice you to keep track of what types you are using in your code. Every type has its own capabilities and some can't do things and some can.

    This will work just fine for you:

    stuff = {'arrows': 41, 'sword': 1, 'dagger': 2, 'torch': 1}
    precious_minerals = {'rubies': 1, 'emeralds': 0, 'sapphires': 0}
    stuff_and_minerals = stuff|precious_minerals
    
    def display_inventory(inventory):
        total_items = 0
        for item, quantity in inventory.items():
            print(f"You have {quantity} {item}")
            total_items += quantity
    
        print(f"You have a total of {total_items} items in your bag.")
    
        if sum(precious_minerals.values()) == 0:
            print('You have no precious minerals.')
        elif sum(precious_minerals.values()) > 0:
            print('You have some precious minerals in your bag.')
            for item, quantity in precious_minerals.items():
                print(f"You have {quantity} {item}")
    
    display_inventory(stuff_and_minerals)