Python Forum
could somebody tell me how I fix an index error?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
could somebody tell me how I fix an index error?
#1
def reciept():
    name =input("what is your name?")
    sipCard =int(input("What is your sip card"))
    twoforone =int(input("How many of the two for one deals would you like?"))
             
#monthly record
    with open ("MonthlyRecord.txt","w")as f:
        f.write (f'{name}\n{sipCard}\n{twoforone}')
    
        with open ("MonthlyRecord.txt","r") as f:
            print(f.read())
            
            
def write():
    shop =input("What shop did you shop at?")
    sipCard =int(input("What is your sip card?"))
    sipCardexipirationDate = int(input("please write the years left before the expiry date (to the nearest year) "))
    cost =float(input("Please enter the total cost of the shop you have shopped at"))
             
#monthly record
    with open ("MonthlyRecord.txt","w")as f:
        f.write (f'{shop}\n{sipCard}\n{sipCardexipirationDate}\n{cost}')
    
    with open ("MonthlyRecord.txt","r") as f:
        print(f.read())
        
     

def read(): 
    lookfor = input ("what is your name?") 
    with open("MonthlyRecord.txt", "r") as f: 
        for line in f.readlines(): 
            sipCard = line.split(" ; ")[0]
            global sipCardexipirationDate
            sipCardexipirationDate = int(line.split(" ; ")[1].replace("\n","")) 
            if sipCard == lookfor: 
                print(sipCardexipirationDate) 
                choice = input("pick 1 or 2") 
                if choice == "1": 
                    write() 
                else: 
                    read() 
                    print ("thank you") 

def discount():
    shopToday= input("Please type the shop you have shopped at ?")
    if shopToday =="1":
        discount1 = totalCost1/10
        finalCost = (totalCost1 - discount1)+ totalCost2 +totalCost3
        print("£:",finalCost)
    if shopToday =="2":
        discount2 = totalCost2/10
        finalCost = (totalCost2 - discount2)+ totalCost1 +totalCost3
        print("£:",finalCost)    
    if shopToday =="3":
        discount3 = totalCost3/10
        finalCost = (totalCost3 - discount3)+ totalCost2 +totalCost1
        print("£:",finalCost)  
        
write()
read()
totalCost1= float(input("what is your1st total cost?"))
totalCost2=float(input("what is your1st total cost?"))
totalCost3=float(input("what is your1st total cost?"))
discount()


#delivery part
shopToday=input("Depending on the day of the week, you can get free delivery. Please enter the day of the week.")
if shopToday == "1" or shopToday== "2":
    location = int(input("Can you please give the distance between the shop to your place in km full number ."))
    if location == 5 or location <5 :
        print ("Okay, your delivery is being sent,right now")
    if location > 5:
        farLocation= input("Sorry we cannot deliver your items, would you like to pick up your purchasements?")
        if farLocation =="yes":
            print ("Okay,we will reserve your purchasements.")
        if farLocation =="no":
            print("Sorry for this issue, we will now delete your purchasements.")

end = input("Thank you for purchasing our products, would you like to recieve an reciept?")
if end == "no":
    print("Then please enjoy your time,goodbye")
if end == "yes":
    reciept()
Error:
File "main.py", line 61, in <module> read() File "main.py", line 35, in read sipCard = int(line.split(" ; ")[1].replace("\n","")) IndexError: list index out of range
Yoriz write Nov-27-2022, 06:57 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Obviously there is line without ;, so when split, there is no element at index 1
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Nov-27-2022, 07:47 PM)buran Wrote: Obviously there is line without ;, so when split, there is no element at index 1

Sorry to bother you here, but this is for my homework and my teacher didnt explain how to use a for loop and etc so could you please put this in more of a simpler way of what i would do to make this part of the code work.
Reply
#4
The problem is not with the loop, but the MonthlyReport.txt file you create. You write to this file in 3 locations and you never use ; e.g. as separator. Unless there is ; in your data, which I doubt, it will throw error because split will return 1-element tuple. i.e. there is only index 0. And even if there is ; in your data, I doubt that is what you want.
Probably you want to use ; as separator, not \n.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how do I fix an index error? MehHz2526 1 1,255 Jan-15-2023, 08:23 PM
Last Post: Gribouillis
  Need help fixing Index Error adesimone 1 1,327 Nov-21-2022, 07:03 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020