There was a problem coding the vending machine with Python. TypeError: '<' not supported between instances of 'tuple' and 'int'

Asked 2 years ago, Updated 2 years ago, 88 views

def show_menu():
    print()
    print ("[Vending Machine Sales Menu]")
    for i in range(0, len(drink_menu)):
        if i%10==0:
            print()
        print("|",i+1,".",drink_menu[i],":",drink_price[i],"|",end=" ")


def buy(num):
    if money < drink_price[num]:
        print("Insufficient balance". Balance: %d" %money)
        return money
    else:
        print (drink_menu[num], "Purchase completed")
        balance = money - drink_price[num]
        left_drink[num] = left_drink[num]-1
        print ("Balance: ", balance)
        return balance,left_drink

def drink(num):
    if left_drink[num]<1:
        print("The drink is out of stock.")
    else:
        pass




if __name__ == '__main__':

    drink_menu = ("Bottled water", "Bottled water", "Lemon water", "Lemon water", "Corn beard tea", "Corn beard tea", "Burdock tea", "Trevi", "Trevi",
                  "Milkies," "Pepsi," "Hot Six," "Cider," "Cocorich (Mango)," "Cocorich (Mango)," "Lipton," "Tropicana Sparkling (Green Apple),"
                  "Tropicana Sparkling (Green Apple)" "Tropicana Sparkling (Grape)" "Kana Chocolate Drink" "Kana Chocolate Drink" "Let's Be" "Let's Be",
                  "Kantata", "Let's Be Cafe Time", "Getoray", "Getoray", "Coco Farm", "Festival House Sikhye")
    drink_price = (600,600,600,1500,1500,1300,1300,1300,1000,1000,800,800,1000,1000,1000,1000,1000,1000,1000,1000,600,600,600,600,1000,1000,800,800,800,800)
    left_drink = [1,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]


    money = 0
    money = int (input ("insert money")

    while True:
        show_menu()
        sel = int (select input("\nmenu number) (exit : 0): "))
        if sel == 0:
            break;
        elif (sel >= 1 and sel <= len(drink_menu)):
            money = buy(sel - 1)
        else:
            print ("Invalid Menu Number")

    print("End of vending machine, return balance %d" %money)

    change_money_1000 = money // 1000
    change_money_500 = (money - (change_money_1000 * 1000)) // 500
    change_money_100 = (money - (change_money_1000 * 1000) - (change_money_500 * 500)) // 100
    print("1000 won:", change_money_1000, "dog", "500 won:", change_money_500, "dog", "100 won:", change_money_100, "dog")

When purchasing a drink, I want to reduce the number of drinks in the left_drink one by one, but if I run it,

Traceback (most recent call last):
  File "C:/Users/JES/.PyCharmCE2019.1/config/scratches/scratch_2.py", line 49, in <module>
    money = buy(sel - 1)
  File "C:/Users/JES/.PyCharmCE2019.1/config/scratches/scratch_2.py", line 11, in buy
    if money < drink_price[num]:
TypeError: '<' not supported between instances of 'tuple' and 'int'

Two errors occur There is an error whether it is done with a tuple or a list, so how can I solve it?

python typeerror

2022-09-22 18:11

1 Answers

This is actually a simple matter. Step by step, I'll tell you where the problem came from.

If you look at the error contents first, it is as follows.

if money < drink_price[num]:<br>
TypeError: unorderable types: tuple() < int()

The error states that money is tuple and drink_price[num] is int type, but it is an error caused by comparison with a comparative operator.

Now, let's check which part of the sentence money was changed to tuple

money = buy(sel - 1)

This is where the value of money is changed.

If you look at the return result of the buy function this time, if the balance is insufficient, the buy function is normally

return money

Returns only the money value due to this part.

However, if you look at the normally processed results that do not run out of balance,

return balance, left_drink

Because of this part, money will receive the value balance, left_drink as a triple at once.

So what's the solution?

There are two options.

Below is the revised code.

def show_menu():
    print()
    print ("[Vending Machine Sales Menu]")
    for i in range(0, len(drink_menu)):
        if i%10==0:
            print()
        print("|",i+1,".",drink_menu[i],":",drink_price[i],"|",end=" ")


def buy(num):
    if money < drink_price[num]:
        print("Insufficient balance". Balance: %d" %money)
        return money
    else:
        print (drink_menu[num], "Purchase completed")
        balance = money - drink_price[num]
        left_drink[num] = left_drink[num]-1
        print ("Balance: ", balance)
        return balance

def drink(num):
    if left_drink[num]<1:
        print("The drink is out of stock.")
    else:
        pass


if __name__ == '__main__':

    drink_menu = ("Bottled water", "Bottled water", "Lemon water", "Lemon water", "Corn beard tea", "Corn beard tea", "Burdock tea", "Trevi", "Trevi",
                  "Milkies," "Pepsi," "Hot Six," "Cider," "Cocorich (Mango)," "Cocorich (Mango)," "Lipton," "Tropicana Sparkling (Green Apple),"
                  "Tropicana Sparkling (Green Apple)" "Tropicana Sparkling (Grape)" "Kana Chocolate Drink" "Kana Chocolate Drink" "Let's Be" "Let's Be",
                  "Kantata", "Let's Be Cafe Time", "Getoray", "Getoray", "Coco Farm", "Festival House Sikhye")
    drink_price = (600,600,600,1500,1500,1300,1300,1300,1000,1000,800,800,1000,1000,1000,1000,1000,1000,1000,1000,600,600,600,600,1000,1000,800,800,800,800)
    left_drink = [1,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]


    money = 0
    money = int (input ("insert money")

    while True:
        show_menu()
        sel = int (select input("\nmenu number) (exit : 0): "))
        if sel == 0:
            break
        elif (sel >= 1 and sel <= len(drink_menu)):
            money = buy(sel - 1)
        else:
            print ("Invalid Menu Number")

    print("End of vending machine, return balance %d" %money)

    change_money_1000 = money // 1000
    change_money_500 = (money - (change_money_1000 * 1000)) // 500
    change_money_100 = (money - (change_money_1000 * 1000) - (change_money_500 * 500)) // 100
    print("1000 won:", change_money_1000, "dog", "500 won:", change_money_500, "dog", "100 won:", change_money_100, "dog")


2022-09-22 18:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.