Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

First time getting this message 'IndexError: list index out of range'

I’m a beginner and this is my first receiving this message "IndexError: list index out of range," can someone please tell me how to fix it? and what exactly did I do wrong? Also if someone can run it and make sure it does what it’s supposed to because I need another person other than me to run it(Professor’s instruction)

This is the output it gave me –

Traceback (most recent call last):
File "", line 74, in
File "", line 24, in user
IndexError: list index out of range

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here’s my code:

print ("Dish No.      Dish Name       Price ")
print (" --------     ---------      ------")
print ("    1         Gang Gai        $10.00")
print ("    2         Pad Thai        $8.75")
print ("    3         Pad Cashew      $9.50")
print ("    4         Pad Prik        $10.25")
print ("    5         Peanut Curry    $9.50")
print ("    6         Curry Noodles   $11.25")  


def user():
    array = [10,8.75,9.50,10.25,9.50,11.25]
    cart = []
    while True:
        x = int(input("Enter the item number you want (1-6):"))
        check = checker(x)
        if check == "wrong number":
            print("Enter a valid number")
            pass
        cart.append(array[x-1])
    
        xx=input("Would you like to order another item( Yes or No)?: ")
        if xx.lower() == "no":
            break
       
    
    checkout(cart)    
    # if xx=='No'.lower():
    #     return array[x-1]
    # else:
    #     return array[x-1]+user(array) 
    
def seniorCitizen():
    print("Are you 65 years or older(Yes or No)? ")
    xsenior = input()
    if xsenior.lower() == "yes":
        senior = True
    else:
        senior = False
    return senior
    
    
def checker(num):
    if num > 6 or num < 1:
        return "wrong number"
    
    
def checkout(cart):
    senior = seniorCitizen()
    titems = 0
    for item in cart:
        titems = titems + item
        
    print("        Bill Information       ")
    print("-------------------------------")
    print("Total of all items:     $",titems)
    if senior == True:
        boomercount = titems * 0.1
        boomercount = round(boomercount, 2)
        print("Total senior discounts:-$", boomercount)
        tax = round((titems-boomercount)*0.06, 2)
        print("Taxes:                  $",tax)
        print("                Bill:   $",  round(((titems-boomercount)+tax), 2))
    else:
        tax = round(titems*0.06, 2)
        print("Taxes:                  $",tax)
        print("                Bill:   $",  round((titems+tax), 2))

    
user()

>Solution :

while True:
    x = int(input("Enter the item number you want (1-6):"))
    check = checker(x)
    if check == "wrong number":
        print("Enter a valid number")
        pass
    cart.append(array[x-1])

The problem is the pass statement. It does not restart the loop — it does nothing at all. So if the user enters a wrong number, it prints an error message but then it keeps going and tries to access array[x-1] which is out of range.

Use continue which will start the loop over, instead of pass.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading