首页 > 解决方案 > 为菜单系统运行代码时出现 2 个错误

问题描述

我正在执行 IGCSE 预发布任务,但每次运行代码时都会出现两个错误,第一个是每次订购第一批食物时,我都必须输入以前的食物代码,因为它不起作用,第二个错误是我经常收到“IndexError: list index out of range”错误。

我的代码:

ItemNames = ['French Fries', '1/4 pound burger', '1/4 pound cheeseburger', '1/2 pound burger', '1/2 pound cheeseburger', 'Medium pizza', 'Medium pizza with extra toppings', 'Large pizza', 'Large pizza with extra toppings', 'Garlic Bread']
ItemPrice=[2.00, 5.00, 5.55, 7.00, 7.50, 9.00, 11.00, 12.00, 14.50, 4.50]
ItemCode=['001','002','003','004', '005', '006', '007', '008', '009', '010']
CartItems=[]
CartQuantity=[]
TotalItemPrice=[]
CartPrice=[]
DailyCost=[]
i=0
while i < len(ItemNames):
    print(ItemNames[i],"|", ItemPrice[i],"$ | Item Code: ", ItemCode[i]," | ")
    i = i+1
i = 0
x = 0
z = 0
while i == 0:
    x = 0
    z = 0
    InputName = input("Please enter the item name which you wish to buy: ")
    while x < len(ItemNames):
        if InputName == ItemNames[x]:
            x = len(ItemNames)
            z = 1
            InputNames = x
        else:
            x=x+1

    if z == 0:
        print("Item not found")
        continue
    else:
        x = 0
        z = 0
        while x < len(ItemCode):
            ItemIndex = ItemNames.index(InputName)
            InputCode = input("Please enter the item code for the item which you wish to buy: ")
            if InputCode == ItemCode[x]:
                x = len(ItemCode)
                z = 1
                InputNames = x
            else:
                x=x+1
            if z == 0:
                print("Item code not found.")
                x = 0
                z = 0
                continue
    x = 0
    ItemQuantity = int(input("Please enter the number of items you wish do buy: "))
    Confirm = input("Please enter y (lower case) to confirm you order, enter any other key to cancel, enter z to finish your order inclusive of this item: ")
    if Confirm == "y" or Confirm == "z":
        CartItems.append(InputName)
        CartQuantity.append(ItemQuantity)
        TotalInputPrice = ItemPrice[ItemIndex]*CartQuantity[ItemIndex]
        TotalItemPrice.append(TotalInputPrice)
        if Confirm == "z":
            CartPrice=sum(TotalItemPrice)
            while x < len(CartItems):
                print(CartItems[x],"|", CartQuantity[x], "Items | Item total: ", TotalItemPrice[x])
                x=x+1
            print("Total: ", CartPrice,"$")
        elif Confirm == "y":
            continue
    else:
        break

我的第一个错误是这样的:

French Fries | 2.0 $ | Item Code:  001  | 
1/4 pound burger | 5.0 $ | Item Code:  002  | 
1/4 pound cheeseburger | 5.55 $ | Item Code:  003  | 
1/2 pound burger | 7.0 $ | Item Code:  004  | 
1/2 pound cheeseburger | 7.5 $ | Item Code:  005  | 
Medium pizza | 9.0 $ | Item Code:  006  | 
Medium pizza with extra toppings | 11.0 $ | Item Code:  007  | 
Large pizza | 12.0 $ | Item Code:  008  | 
Large pizza with extra toppings | 14.5 $ | Item Code:  009  | 
Garlic Bread | 4.5 $ | Item Code:  010  | 
Please enter the item name which you wish to buy: French Fries
Please enter the item code for the item which you wish to buy: 001
Please enter the number of items you wish do buy: 2
Please enter y (lower case) to confirm you order, enter any other key to cancel, enter z to finish your order inclusive of this item: y
Please enter the item name which you wish to buy: Medium pizza
Please enter the item code for the item which you wish to buy: 006
Item code not found.
Please enter the item code for the item which you wish to buy: 001

和我的错误:

French Fries | 2.0 $ | Item Code:  001  | 
1/4 pound burger | 5.0 $ | Item Code:  002  | 
1/4 pound cheeseburger | 5.55 $ | Item Code:  003  | 
1/2 pound burger | 7.0 $ | Item Code:  004  | 
1/2 pound cheeseburger | 7.5 $ | Item Code:  005  | 
Medium pizza | 9.0 $ | Item Code:  006  | 
Medium pizza with extra toppings | 11.0 $ | Item Code:  007  | 
Large pizza | 12.0 $ | Item Code:  008  | 
Large pizza with extra toppings | 14.5 $ | Item Code:  009  | 
Garlic Bread | 4.5 $ | Item Code:  010  | 
Please enter the item name which you wish to buy: French Fries
Please enter the item code for the item which you wish to buy: 001
Please enter the number of items you wish do buy: 2
Please enter y (lower case) to confirm you order, enter any other key to cancel, enter z to finish your order inclusive of this item: y
Please enter the item name which you wish to buy: Medium pizza
Please enter the item code for the item which you wish to buy: 006
Item code not found.
Please enter the item code for the item which you wish to buy: 001
Please enter the number of items you wish do buy: 2
Please enter y (lower case) to confirm you order, enter any other key to cancel, enter z to finish your order inclusive of this item: z
Traceback (most recent call last):
  File (private), line 54, in <module>
    TotalInputPrice = ItemPrice[ItemIndex]*CartQuantity[ItemIndex]
IndexError: list index out of range

标签: pythonarrays

解决方案


推荐阅读