Python Forum

Full Version: could somebody tell me how I fix an index error?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
Obviously there is line without ;, so when split, there is no element at index 1
(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.
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.