Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: unsupported operand type(s) for *: 'dict' and 'int'

I get the error in the title when I run the following code.

amount = int(input('How many packs do you want?'))

pack = {'nuts':4.0,
        'bolts':300.0,
        'screws':140.0,
        'wire(m)':3.5}

for key,val in pack.items():
    total = pack * amount
    print(total,key)

I assume that this is because the values in the dictionary are not integer's. how do I fix my code so it doesn't give me this error.

It should print the number of things the person would receive, for example, if someone ordered 2 packs it would print:

8.0 nuts 600.0 bolts 280.0 screws 7.0 wire(m)

like image 602
james hughes Avatar asked Aug 31 '25 17:08

james hughes


1 Answers

You are calculating the total wrong, you need to multiply with val and not with pack (which is a dict). Use the below instead (total = val * amount instead of total = pack * amount):

for key,val in pack.items():
    total = val * amount
    print(total,key)

So no, the reason is not that the values in the dictionary are not integers.

like image 100
Anshul Goyal Avatar answered Sep 02 '25 05:09

Anshul Goyal