'(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.
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()
Solution 1:[1]
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.
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()
Solution 2:[2]
For the list of out range, you don't append to qty_Count. You can also get rid of elif just use _Order number as an index. 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)
Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Tomáš Šturm |
| Solution 3 | Susan |
