Home > Net >  '<' not supported between instances of 'dict' and 'int'
'<' not supported between instances of 'dict' and 'int'

Time:02-20

I newbie here. English is not my native language so please excuse mispronounced words and any grammar mistakes. I'm trying to write a code for a vending machine with special conditions. I have a problem with inv_coins.

Inv_coins = amount of banknotes for change.

Getting the following error with my code:

 while inv_coins < price:
                inv_coins = inv_coins   float(input('insert '   str(price - inv_coins)   ': 

TypeError: '<' not supported between instances of 'dict' and 'int'

How should I edit this code for working my vending machine?

def vend():
'''
inv_data = {'Chips':{'stock':50, 'price':11}, 
        'Water':{'stock':50, 'price':7}, 
        'Candies':{'stock':50, 'price':8}, 
        'Sandwich':{'stock':50, 'price':27}}
inv_coins = {100:0, 50:0, 10:20, 5:20, 2:20, 1:20}
'''

a = {'item': 'Chips', 'price': 11, 'stock': 50}
b = {'item': 'Water', 'price': 7, 'stock': 50}
c = {'item': 'Candies', 'price': 8, 'stock': 50}
d = {'item': 'Sandwich', 'price': 27, 'stock': 50}
items = [a, b, c, d]
cim = {100:0, 50:0, 10:20, 5:20, 2:20, 1:20}

print('Hi there! This is the vending machine of Felix Seletskiy \n@@@@@@@@@@@@@@@')

# 1. Here we offer a menu by showing items and prices
def show(items):
    print('\nitems available \n***************')

    for item in items:      
        if item.get('stock') == 0:
            items.remove(item)
    for item in items:
        print(item.get('item'), item.get('price'))

    print('***************\n')
# 2. Here we take an order to suggest to place another order
continueToBuy = True
# have user choose item
while continueToBuy == True:
    show(items)
    selected = input('select item: ')
    for item in items:
        if selected == item.get('item'):
            selected = item               
            price = selected.get('price')
            while inv_coins < price:
                inv_coins = inv_coins   float(input('insert '   str(price - inv_coins)   ': '))   

            print('you got '   selected.get('item'))
            # 4. Here we update the stock after processing the current order
            selected['stock'] -= 1
            inv_coins-= price
            print('cash remaining: '   str(inv_coins))
            a = input('buy something else? (y/n): ')
            if a == 'n':
                continueToBuy = False
                # 3. Here we calculate the price and update the price based on coins input
                # 4. Here we calculate the change based on coins stock
                if cim != 0:
                    print(str(cim)   ' refunded')
                    cim = 0
                    print('thank you, have a nice day!\n')
                    break                        
                else:
                    print('thank you, have a nice day!\n')
                    break  
            else:
                continue

CodePudding user response:

In your line while inv_coins < price:, you're comparing inv_coins, which is a dictionary, and coins, which is an integer number. That comparison doesn't work. You should not compare the dictionary itself to the number but rather get the corresponding number. I suppose inv_coin means the inventory of coins, so you could loop over that dictionary to get the number of coins of each denomination, multiply it by the value of that denomination (e.g., if you have twenty quarters that would be 20 * $0.25 = $5), add those up, and that gives you an integer number against which you can compare another integer number.

CodePudding user response:

The problem is that you can't just use inv_coins (which is a dictionary) and compare it to an integer number.

Whenever you want to access inv_coins you have to specify which value exactly you want to access and provide its key in square brackets.

e.g.:

# dictionary with key:value pairs
inv_coins = {100:0, 50:0, 10:20, 5:20, 2:20, 1:20}

# find out how many coins with key 10 there are in the inv_coins dict:
print(inv_coins[10])
> 20

# add a coin with key 100 to the inv_coins dict:
inv_coins[100]  = 1
print(inv_coins[100])
> 1
print(inv_coins)
> {100:1, 50:0, 10:20, 5:20, 2:20, 1:20}

What exactly are you trying to archive with the while-loop while inv_coins < price:? Do you want to stay in this loop until enough coins have been added to the coin inventory that the price is covered?

This might be generally problematic because you are missing a reference to what the inventory was before the customer started inserting coins. So it would be better to use an extra variable to know how much was paid so far then and once that covers the price you proceed.

CodePudding user response:

the error is telling you exactly what is the problem, you are trying to compare a dictionary and an integer and python don't like that.

  • Related