简体   繁体   English

(IndexError:列表索引超出范围)

[英](IndexError: list index out of range)

While making this simple program, I encountered this error that seems to be related to lists.在制作这个简单的程序时,我遇到了这个似乎与列表有关的错误。 However, I don't really know how to fix it properly.但是,我真的不知道如何正确修复它。

PS: if you could give me a few pointers or maybe ways I could make my program better that be cool. PS:如果你能给我一些建议或者我可以让我的程序变得更好的方法,那就太酷了。

def getOrder():
    mainDish = ["Burger", "Pizza", "Taco", "Italian Sandwich"]
    mainDish_Price = [3.55, 7.99, 12.99, 5.99]
    drink = ["Cola", "Pepsi", "Sprite", "Water"]
    drink_Price = [0.80, 0.60, 0.30, 0.20]
    sideDish = ["French Fries", "Onion Rings", "Salad", "Chips"]
    sideDish_Price = [1.25, 2.99, 1.75, 2.25]

    myOrder_Count = []
    myOrder_Cost = []
    qty_Count = []

    fullPrice = 0
    total_Drink_Order = 0
    total_MainDish_Order = 0
    total_SideDish_Order = 0
    count = 0
    i = 0

    while(True):
            print("AI : Resturant Bot Options.")
            print("AI : Choose [1] For Main Dish.")
            print("AI : Choose [2] For Sides.")
            print("AI : Choose [3] For Drinks.")
            print("AI : Choose [4] For Getting Reciept.")
            print("AI : Choose [5] For Exit.")

            choice = int(input("Select Your Choice : "))
            
            if(choice == 1):
                print("\n" + "AI:"
                    "\n\n1 : Burger               $3.55"
                    "\n\n2 : Pizza                $7.99"
                    "\n\n3 : Taco                 $12.99"
                    "\n\n4 : Italian Sandwich     $5.99")

                mainDish_Order = int(input("\n\nAI: Select Your Choice. "))
                qty = int(input("AI: Please Enter The Quantity.  "))
                if(mainDish_Order == 1):
                    myOrder_Count.append(mainDish[0])
                    myOrder_Cost.append(mainDish_Price[0])
                    count += 1
                    total_MainDish_Order += mainDish_Price[0] * qty
                
                elif(mainDish_Order == 2):
                    myOrder_Count.append(mainDish[1])
                    myOrder_Cost.append(mainDish_Price[1])
                    count += 1
                    total_MainDish_Order += mainDish_Price[1] * qty
                
                elif(mainDish_Order == 3):
                    myOrder_Count.append(mainDish[2])
                    myOrder_Cost.append(mainDish_Price[2])
                    count += 1
                    total_MainDish_Order += mainDish_Price[2] * qty
                
                elif(mainDish_Order == 4):
                    myOrder_Count.append(mainDish[3])
                    myOrder_Cost.append(mainDish_Price[3])
                    count += 1
                    total_MainDish_Order += mainDish_Price[3] * qty
                
            if(choice == 2):
                print("\n" + "AI: " + 
                    "\n\n1 : French Fries               $1.25"
                    "\n\n2 : Onion Rings                $2.99"
                    "\n\n3 : Salad                      $1.75"
                    "\n\n4 : Chips                      $2.25")

                sideDish_Order = int(input("AI : Please Select Your Choice."))
                qty = int(input("AI: Please Enter The Quantity : "))
                if(sideDish_Order == 1):
                    myOrder_Count.append(sideDish[0])
                    myOrder_Cost.append(sideDish_Price[0])
                    count += 1
                    total_SideDish_Order += sideDish_Price[0] * qty
                    
                elif(sideDish_Order == 2):
                    myOrder_Count.append(sideDish[1])
                    myOrder_Cost.append(sideDish_Price[1])
                    count += 1
                    total_SideDish_Order += sideDish_Price[1] * qty
                    
                elif(sideDish_Order == 3):
                    myOrder_Count.append(sideDish[2])
                    myOrder_Cost.append(sideDish_Price[2])
                    count += 1
                    total_SideDish_Order += sideDish_Price[2] * qty
                    
                elif(sideDish_Order == 4):
                    myOrder_Count.append(sideDish[3])
                    myOrder_Cost.append(sideDish_Price[3])
                    count += 1
                    total_SideDish_Order += sideDish_Price[3] * qty
                
            if(choice == 3):
                print("\n" + "AI :" 
                    "\n\n1 : Cola                   $0.80"
                    "\n\n2 : Pepsi                  $0.60"
                    "\n\n3 : Sprite                 $0.40"
                    "\n\n4 : Water                  $0.20")

                drink_Order = int(input("AI : Select Your Choice : "))
                qty = int(input("AI: Please Enter The Quantity : "))
                if(drink_Order == 1):
                    myOrder_Count.append(drink[0])
                    myOrder_Cost.append(drink_Price[0])
                    count += 1
                    total_Drink_Order += drink_Price[0] * qty
                    
                elif(drink_Order == 2):
                    myOrder_Count.append(drink[1])
                    myOrder_Cost.append(drink_Price[1])
                    count += 1
                    total_Drink_Order += drink_Price[1] * qty
                    
                elif(drink_Order == 3):
                    myOrder_Count.append(drink[2])
                    myOrder_Cost.append(drink_Price[2])
                    count += 1
                    total_Drink_Order += drink_Price[2] * qty
                    
                elif(drink_Order == 4):
                    myOrder_Count.append(drink[3])
                    myOrder_Cost.append(drink_Price[3])
                    count += 1
                    total_Drink_Order += drink_Price[3] * qty
            fullPrice = total_MainDish_Order + total_SideDish_Order + total_Drink_Order

            if(choice == 4):
                print("..")
                time.sleep(2)
                print("...")
                time.sleep(3)
                print("....")
                time.sleep(3)
                
                print("AI: Here's Your Reciept")
                print("            ")
                print("**************")       
                while(i < count):
                    print(f"Items : {myOrder_Count[i]}")
                    print(f"Cost : ${myOrder_Cost[i]}")
                    print(f"qty : {qty_Count[i]} \n")
                    i += 1
                
                print("**************")
                print(f"AI: The Final Cost Is ${fullPrice}")
                exit()

            if(choice == 5):
                print("AI: System Shutdown.")
                exit()

getOrder()

the problem in your code is that the list qty_Count is always empty so when the code tries to run the line print(f"qty: {qty_Count[i]} \n") when the list is empty it doesn't find the index so the program crashes.您的代码中的问题是列表qty_Count始终为空,因此当代码尝试在列表为空时运行print(f"qty: {qty_Count[i]} \n")行时,它找不到索引所以程序崩溃了。

I couldn't find where you store any data in that list so either find a way to solve that - by inserting data to the list - or remove that part from being printed.我找不到您在该列表中存储任何数据的位置,因此要么找到解决该问题的方法 - 通过将数据插入列表 - 要么从打印中删除该部分。

Edit: This code works without problems.编辑:此代码可以正常工作。

import time
def getOrder():
    mainDish = ["Burger", "Pizza", "Taco", "Italian Sandwich"]
    mainDish_Price = [3.55, 7.99, 12.99, 5.99]
    drink = ["Cola", "Pepsi", "Sprite", "Water"]
    drink_Price = [0.80, 0.60, 0.30, 0.20]
    sideDish = ["French Fries", "Onion Rings", "Salad", "Chips"]
    sideDish_Price = [1.25, 2.99, 1.75, 2.25]

    myOrder_Count = []
    myOrder_Cost = []
    qty_Count = []

    fullPrice = 0
    total_Drink_Order = 0
    total_MainDish_Order = 0
    total_SideDish_Order = 0
    count = 0
    i = 0

    while(True):
            print("AI : Resturant Bot Options.")
            print("AI : Choose [1] For Main Dish.")
            print("AI : Choose [2] For Sides.")
            print("AI : Choose [3] For Drinks.")
            print("AI : Choose [4] For Getting Reciept.")
            print("AI : Choose [5] For Exit.")

            choice = int(input("Select Your Choice : "))
            
            if(choice == 1):
                print("\n" + "AI:"
                    "\n\n1 : Burger               $3.55"
                    "\n\n2 : Pizza                $7.99"
                    "\n\n3 : Taco                 $12.99"
                    "\n\n4 : Italian Sandwich     $5.99")

                mainDish_Order = int(input("\n\nAI: Select Your Choice. "))
                qty = int(input("AI: Please Enter The Quantity.  "))
                qty_Count.append(qty)
                if(mainDish_Order == 1):
                    myOrder_Count.append(mainDish[0])
                    myOrder_Cost.append(mainDish_Price[0])
                    count += 1
                    total_MainDish_Order += mainDish_Price[0] * qty
                
                elif(mainDish_Order == 2):
                    myOrder_Count.append(mainDish[1])
                    myOrder_Cost.append(mainDish_Price[1])
                    count += 1
                    total_MainDish_Order += mainDish_Price[1] * qty
                
                elif(mainDish_Order == 3):
                    myOrder_Count.append(mainDish[2])
                    myOrder_Cost.append(mainDish_Price[2])
                    count += 1
                    total_MainDish_Order += mainDish_Price[2] * qty
                
                elif(mainDish_Order == 4):
                    myOrder_Count.append(mainDish[3])
                    myOrder_Cost.append(mainDish_Price[3])
                    count += 1
                    total_MainDish_Order += mainDish_Price[3] * qty
                
            if(choice == 2):
                print("\n" + "AI: " + 
                    "\n\n1 : French Fries               $1.25"
                    "\n\n2 : Onion Rings                $2.99"
                    "\n\n3 : Salad                      $1.75"
                    "\n\n4 : Chips                      $2.25")

                sideDish_Order = int(input("AI : Please Select Your Choice."))
                qty = int(input("AI: Please Enter The Quantity : "))
                qty_Count.append(qty)
                if(sideDish_Order == 1):
                    myOrder_Count.append(sideDish[0])
                    myOrder_Cost.append(sideDish_Price[0])
                    count += 1
                    total_SideDish_Order += sideDish_Price[0] * qty
                    
                elif(sideDish_Order == 2):
                    myOrder_Count.append(sideDish[1])
                    myOrder_Cost.append(sideDish_Price[1])
                    count += 1
                    total_SideDish_Order += sideDish_Price[1] * qty
                    
                elif(sideDish_Order == 3):
                    myOrder_Count.append(sideDish[2])
                    myOrder_Cost.append(sideDish_Price[2])
                    count += 1
                    total_SideDish_Order += sideDish_Price[2] * qty
                    
                elif(sideDish_Order == 4):
                    myOrder_Count.append(sideDish[3])
                    myOrder_Cost.append(sideDish_Price[3])
                    count += 1
                    total_SideDish_Order += sideDish_Price[3] * qty
                
            if(choice == 3):
                print("\n" + "AI :" 
                    "\n\n1 : Cola                   $0.80"
                    "\n\n2 : Pepsi                  $0.60"
                    "\n\n3 : Sprite                 $0.40"
                    "\n\n4 : Water                  $0.20")

                drink_Order = int(input("AI : Select Your Choice : "))
                qty = int(input("AI: Please Enter The Quantity : "))
                qty_Count.append(qty)
                if(drink_Order == 1):
                    myOrder_Count.append(drink[0])
                    myOrder_Cost.append(drink_Price[0])
                    count += 1
                    total_Drink_Order += drink_Price[0] * qty
                    
                elif(drink_Order == 2):
                    myOrder_Count.append(drink[1])
                    myOrder_Cost.append(drink_Price[1])
                    count += 1
                    total_Drink_Order += drink_Price[1] * qty
                    
                elif(drink_Order == 3):
                    myOrder_Count.append(drink[2])
                    myOrder_Cost.append(drink_Price[2])
                    count += 1
                    total_Drink_Order += drink_Price[2] * qty
                    
                elif(drink_Order == 4):
                    myOrder_Count.append(drink[3])
                    myOrder_Cost.append(drink_Price[3])
                    count += 1
                    total_Drink_Order += drink_Price[3] * qty
            fullPrice = total_MainDish_Order + total_SideDish_Order + total_Drink_Order

            if(choice == 4):

                
                print("AI: Here's Your Reciept")
                print("            ")
                print("**************")       
                while(i < count):
                    print(f"Items : {myOrder_Count[i]}")
                    print(f"Cost : ${myOrder_Cost[i]}")
                    print(f"qty : {qty_Count[i]} \n")
                    i += 1
                
                print("**************")
                print(f"AI: The Final Cost Is ${fullPrice}")
                exit()

            if(choice == 5):
                print("AI: System Shutdown.")
                exit()

getOrder()

For the list of out range, you don't append to qty_Count.对于超出范围的列表,您不要 append 到 qty_Count。 You can also get rid of elif just use _Order number as an index.您也可以摆脱 elif 只需使用 _Order 编号作为索引。 Also, iterate over print when you only change food and price.此外,当您只更改食物和价格时,重复打印。 Example:例子:

        if(choice == 1):
            print("\n" + "AI:", end="")
            for food, price in zip(mainDish, mainDish_Price):
                print(f"\n\n1 : {food:20} ${price}")

            mainDish_Order = int(input("\n\nAI: Select Your Choice. "))
            qty = int(input("AI: Please Enter The Quantity.  "))
            myOrder_Count.append(mainDish[mainDish_Order-1])
            myOrder_Cost.append(mainDish_Price[mainDish_Order-1])
            count += 1
            total_MainDish_Order += mainDish_Price[mainDish_Order-1] * qty
            qty_Count.append(qty)

I quite enjoyed looking at your script, I remember doing similar stuff.我很喜欢看你的剧本,我记得做过类似的事情。 If you are at the stage of using functions, and it looks like you are, then I would suggest that you go with that a bit more and make your program more modular and break it down instead of everything in one function, it makes debugging a lot easier.如果您正处于使用函数的阶段,并且看起来像这样,那么我建议您使用更多的 go 并使您的程序更加模块化并将其分解而不是一个 function 中的所有内容,它使调试成为容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM