0

Hi I am a beginner in python and I am currently using Automate the boring stuff with python. This is my second book learning python. I practiced doing this code (transferring items in list to a dictionary) my code worked the way I wanted it to work but there is a 'none' at the bottom. Can someone enlighten my tiny brain on how to fix this please?

def addToInventory(inventory, addedItems):

    for i in addedItems:
        if i == 'gold coin':
            inventory['gold coin'] += 1
        else:
            inventory.setdefault(i, 0)
            inventory[i] += 1

    print('Inventory:')
    total_item = 0

    for k, v in inventory.items():
        print(str(v) + ' ' + k)
        total_item += v

    print('\nTotal number of items: ' + str(total_item))


inv = {'gold coin': 42, 'rope': 1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
print(addToInventory(inv, dragonLoot))

This is the result: https://i.stack.imgur.com/fP1Ol.png

user109503
  • 41
  • 1
  • 7
  • 1
    `addToInventory()` contains no return-statement, so when it is complete it returns `None` which is the default return value. You print the result of that function call so you print `None`. Just called the function without enclosing it in `print()` and you'll no longer get the undesired output. – Steven Rumbalski Aug 09 '20 at 02:36
  • Does this answer your question? [Function returns None without return statement](https://stackoverflow.com/questions/7053652/function-returns-none-without-return-statement) – Austin Aug 09 '20 at 02:37
  • In other words, change `print(addToInventory(inv, dragonLoot))` to `addToInventory(inv, dragonLoot)` – Steven Rumbalski Aug 09 '20 at 02:41
  • ohh thats it thank you so much! – user109503 Aug 09 '20 at 02:42

3 Answers3

1

Because you are not returning anything from addToInventory function. If you return total_item then it won't print None. After updating your code, it would look something like this:

def addToInventory(inventory, addedItems):

    for i in addedItems:
        if i == 'gold coin':
            inventory['gold coin'] += 1
        else:
            inventory.setdefault(i, 0)
            inventory[i] += 1
    
    print('Inventory:')
    total_item = 0
    
    for k, v in inventory.items():
        print(str(v) + ' ' + k)
        total_item += v
    
    print('\nTotal number of items: ' + str(total_item))
    return total_item
Jack Taylor
  • 5,588
  • 19
  • 35
Shubham Jain
  • 33
  • 1
  • 2
1

The logic of your function is sound. It returns None because unlike some other languages, Python functions return None by default, unless you tell it to explicitly return something. For example:

def func_A():
    x=5

def func_B():
    x=5
    return x

print(func_A())   # prints "None"
print(func_B())   # prints 5

Therefore, try this instead:

def addToInventory(inventory, addedItems):

    for i in addedItems:
        if i == 'gold coin':
            inventory['gold coin'] += 1
        else:
            inventory.setdefault(i, 0)
            inventory[i] += 1

    print('Inventory:')
    total_item = 0

    for k, v in inventory.items():
        print(str(v) + ' ' + k)
        total_item += v

    print('\nTotal number of items: ' + str(total_item))
    return total_item # this is key
Dharman
  • 30,962
  • 25
  • 85
  • 135
1

In Python, functions always return something. If you use the return keyword explicitly, then the function will return that value. If you don't use the return keyword, then the function will return None by default.

This is what is happening in your addToInventory function - it returns None. So when you do print(addToInventory(inv, dragonLoot)) at the end of your script, you are printing the None that the function returns. The part that prints the total number of items happens before that, inside the addToInventory function itself.

To return the total number of items from the function and print that, you might do something like this:

def addToInventory(inventory, addedItems):

    for i in addedItems:
        if i == 'gold coin':
            inventory['gold coin'] += 1
        else:
            inventory.setdefault(i, 0)
            inventory[i] += 1

    print('Inventory:')
    total_items = 0

    for k, v in inventory.items():
        print(str(v) + ' ' + k)
        total_items += v

    return total_items


inv = {'gold coin': 42, 'rope': 1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
print('\nTotal number of items: ' + str(addToInventory(inv, dragonLoot)))
Jack Taylor
  • 5,588
  • 19
  • 35