#python #arrays
Вопрос:
Я выполняю задачу IGCSE перед выпуском, но каждый раз, когда я запускаю свой код, я получаю две ошибки, первая из которых заключается в том, что каждый раз, когда я заказываю первую партию продуктов питания, я должен вводить предыдущий код продуктов питания, так как он не будет работать, а вторая ошибка заключается в том, что я постоянно получаю ошибку «IndexError: индекс списка вне диапазона».
Мой код:
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 lt; 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 lt; 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 lt; 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 lt; 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 lt;modulegt; TotalInputPrice = ItemPrice[ItemIndex]*CartQuantity[ItemIndex] IndexError: list index out of range