Home > Blockchain >  trying to calculate the total overall weight of available items in my dictionary's not sure how
trying to calculate the total overall weight of available items in my dictionary's not sure how

Time:04-02

weight = {"pencil" : "10", "pen" : "20", "paper" : "4", "eraser" : "80" }

available = {"pen" : "3", "pencil" : "5", "eraser" : "2", "paper" : "10"}

overall_weight = 0

for key, value in weight.items():

if key in available:
    overall_weight = weight.values() * available.values()
    print (overall_weight)

print("Overall weight:", overall_weight)

CodePudding user response:

You need to cast the values to int. This should work:

weight = {"pencil" : "10", "pen" : "20", "paper" : "4", "eraser" : "80" }
available = {"pen" : "3", "pencil" : "5", "eraser" : "2", "paper" : "10"}

overall_weight = 0

for key, value in weight.items():
    if key in available:
        currA = int(value)
        overall_weight  = int(available[key]) * int(value)
        print (overall_weight)

print("Overall weight:", overall_weight)
  • Related