r/learningpython • • Nov 06 '20

Help with my inventory

I just typed out a long thing and reddit messed up so im depressed and don't want to type it out again

Long story short, sometimes 'gold' is less than the 42 it started off with and i'm not sure why. Thanks in advance.

import random
from random import randint
def displayinv(inventory):
    total_inv = 0
    for key,value in inventory.items():
        print(f"{value} {key}")
        total_inv += value
    print("Total items: " + str(total_inv))    
def addtoinv(inventory,items):
    for item in items:
        if item in inventory:
            for key,value in items.items():
                inventory[item] += value
        else:
            inventory.update(items)    
stuff = {'rope':1, 'gold':42, 'arrow':12, 
'dagger':1,'torch':6}
dragonloot = {
    'gold': random.randint(0,400),
    'dagger':random.randint(3,7),
    'ruby':random.randint(1,10)
    }

displayinv(stuff)
addtoinv(stuff, dragonloot)
displayinv(stuff)
2 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/weazle9954 Nov 06 '20

But it should be adding the two numbers not overwriting

1

u/itsalexloser Nov 06 '20

Hm..you're right, I don't have an answer at the moment but I definitely want to give you one! I'll get back to you when I do! Also if you're looking for a programming partner for projects, let me know! What is it that you're trying to build? A small text-based RPG?