【问题标题】:Problems reading and calculating items in list读取和计算列表中的项目时出现问题
【发布时间】:2021-07-04 21:08:51
【问题描述】:

我正在尝试使用菜单来接受附加一个空列表作为购物车的菜单选项。当列表完成后,如果需要,我可以选择添加更多列表。最后我应该计算购物车的总数、物品的总数和总价。第一个问题是购物车的计算是错误的,因为它将每个新条目都视为列表而不是 na 项目,这表明每个购物车的项目计数也是错误的。除此之外,我在尝试计算最终价格时得到“TypeError:+:'int'和'str'不支持的操作数类型”,我只是不确定该怎么做

def main():
    #Flag for full checking out or not
    checkout = False
    #Flag to start a new cart or not
    new_cart = True
    #Placeholder list
    cart = []
    #List of items
    book_list = [['My Own Words', 18.00], ['Grant', 24.50], ['The Overstory', 18.95], ['Becoming', 18.99]]
    elec_list = [['HP Laptop', 429.50], ['Eyephone', 790.00], ['Bose Speakers', 220.00]]
    cloth_list = [['T-shirt', 9.50], ['Shoes', 45.00], ['Pants', 24.00], ['Nationals Hat', 32.00]]
    groc_list = [['Coho Salmon', 12.50], ['Spaghetti', 2.75], ['Milk', 3.99], ['Eggs', 1.99], ['Flat Tire Ale', 9.95]]
    while checkout == False or new_cart == True:
        #Main Menu
        if checkout == False:
            #Main Item menu
            print("""
                1 - Books
                2 - Electronics
                3 - Clothes
                4 - Groceries
                c - Continue to checkout
                """)
            choice = input("Select one of the categories or checkout(1-4 or 'c'): ")
            #Variable to return user to past menu
            Return = False 
            if choice == '1':
                while Return == False:
                    #Sub item menu
                    print("""
                            1 - "My Own Words", $18.00
                            2 - "Grant", $24.50
                            3 - "The Overstory", $18.95
                            4 - "Becoming", $18.99
                            x - return to menu
                            """)
                    item = input("Please select from the menu or go back to the categories: ")
                    if item == '1' or item == '2' or item =='3' or item == '4':
                        #Adds item onto the the cart
                        cart.append(book_list[int(item)-1]) 
                    elif item == 'x':
                        #Returns user to main menu
                        Return = True
                    else: print("Invalid input try again")
            elif choice == '2':
                while Return == False:
                    #Sub item menu
                    print("""
                            1 - HP Laptop, $429.50
                            2 - EyePhone 10, $790.00
                            3 - Bose 20 Speakers, $220.00
                            x - return to menu
                            """)
                    item = input("Please select from the menu or go back to the categories: ")
                    if item == '1' or item == '2' or item == '3':
                        #Adds item onto the the cart
                        cart.append(elec_list[int(item)-1])
                    elif item == 'x':
                        Return = True
                    else:
                        print("Invalid input try again")
            elif choice == '3':
                while Return == False:
                    #Sub item menu
                    print("""
                            1 - T-shirt, $9.50
                            2 - Shoes, $45.00
                            3 - Pants, $24.00
                            4 - Nationals Hat, $32.00
                            x - return to menu
                            """)
                    item = input("Please select from the menu or go back to the categories: ")
                    if item == '1' or item == '2' or item == '3':
                        #Adds item onto the the cart
                        cart.append(cloth_list[int(item)-1])
                    elif item == 'x':
                        Return = True
                    else:
                        print("Invalid input try again")
            elif choice == '4':
                while Return == False:
                    #Sub item menu
                    print("""
                        1 – Coho Salmon, $12.50
                        2 − Spaghetti, $2.75
                        3 – Milk, $3.99
                        4 – Eggs, $1.99
                        5 – Flat Tire Ale, $9.95
                        x - return to menu
                        """)
                    item = input("Please select from the menu or go back to the categories: ")
                    if item == '1' or item == '2' or item == '3' or item == '4' or item == '5':
                        #Adds item onto the the cart
                        cart.append(groc_list[int(item)-1])
                    elif item == 'x':
                        Return = True
                    else:
                        print("Invalid input try again")
            elif choice == 'c':
                checkout = True
                print(cart)
            else: print("Invalid input, please try again!")
        else:
            print("Do you want a new cart y/n")
            choice = input()
            if choice == 'y':
                checkout = False
                #Create new cart
                cart.append([])
            elif choice == 'n':
                #Proceed to item summary
                new_cart = False
            else:
                print("Invalid Option, Choose again")
    #Print total number of carts
    print("Total number of carts :",len(cart)) 
    for v in range(len(cart)):
        #Increment for each existing cart
        print("Cart",v+1)
        #Add total number of items within every cart
        print("Total Number of items:",len(cart[v]))
        #Add total price of items within every cart
        print("Total cost of the items: $",sum(cart[v]))
    
main()

【问题讨论】:

    标签: python list


    【解决方案1】:

    您的购物车包含一个项目列表,这些项目本身就是数组:

    例如,我运行了您的代码,您的购物车如下所示:

    [['My Own Words', 18.0], ['My Own Words', 18.0], ['My Own Words', 18.0], ['My Own Words', 18.0]]

    您正在尝试对数组的位置应用总和,例如,当您这样做时 sum(cart[0]) 你在 ['My Own Words', 18.0] 上调用 sum ,所以你的代码尝试这样做:

    'My Own Words'+18 给出类型错误。

    如果你只需要总价,你可以只附加价格而不是整个项目,或者你可以简单地将价格附加到一个单独的数组中并调用 sum

    【讨论】:

    • 我明白你的意思。当我在购物车列表中看到它时,我会看到价格所在的位置,但是每当我执行 sum(cart[0][1]) 时,它仍然没有通过。你能告诉我这是为什么吗?
    • 因为购物车包含[name, price] 的列表,您可以使用zip(*cart),它将输出两个列表:一个名称和一个进程。类似total = sum(zip(*cart)[1])
    • 我试过了,但是我得到了错误“TypeError: 'zip' object is not subscriptable”,即使在尝试使用total = sum(list(zip(*cart)[1]))将其压缩为列表之后也是如此。
    • 你的 [1] 位置不对,你想做 sum(list(zip(*cart))[1])
    • 即使在那之后我得到错误 TypeError: unsupported operand type(s) for +: 'int' and 'list'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多