-4

The assignment was to let the user choose to either read the data file or to write to the data file. I figured out how to write to it but I cannot understand my current error: that my list is out of range.

Can someone tell me how my list is out of range?

I've done everything right, I believe, but I'm honestly not sure how my list is out of range. Please help me guys. Thanks!

#Run again

useAgain=bool(True)

while useAgain:

#Display Options
    print("\nYou may either read or write to the file:\n"\
          "\n'1' - To read and display records in the data file."\
          "\n'2' - To order.\n")

    choice=str(input("Please enter your choice here: "))


    def func_read():
        cs7Assn7=open("cs7Assn7.txt",'r')
        line=cs7Assn7.readline()
        places=line.split('+')
        userID=places[0]
        widgetsNum=places[1]
        gidgetsNum=places[2]
        doodadsNum=places[3]
        print("Customer ID: ",userID,"\nNumber of Widgets: ",widgetsNum,\
              "\nNumber of Gidgets: ",gidgetsNum,"\nNumber of Doodads: ",\
              doodadsNum,"\n")

    def func_order():
        userID=str(input("Please enter your user ID(2 letters"\
                         " followed by 3 numbers: "))
        widgetsNum=abs(int(input("Number of Widgets you would"\
                                   " like to order: ")))
        gidgetsNum=abs(int(input("Number of Gidgets you would"\
                                   " like to order: ")))
        doodadsNum=abs(int(input("Number of Doodads you would"\
                                   " like to order: ")))

        items=open("cs7Assn7.txt",'a')
        items.write(str(userID) + str(abs(widgetsNum)) + str(abs(gidgetsNum))\
                    + str(abs(doodadsNum)) + '\n')
        items.close()

    if choice=="1":
        func_read()
    elif choice=="2":
        func_order()
    useAgain=str(input("\nWould you like to run this code again? Type 'Y' to"\
                       " run or 'N' to stop: "))
    useAgain=useAgain.lower()
    if useAgain !="y":
        useAgain=bool(False)
  • At which line is the error? – shruti1810 May 19 '15 at 07:19
  • We need more details than this. What line? What have you tried? What are some examples? – Tux May 19 '15 at 07:19
  • can you paste the traceback of the error? – sachin saxena May 19 '15 at 07:21
  • 2
    I assume you are getting the error while reading the file. Could you provide the line/row in the file `cs7Assn7.txt` for which you are getting the error? – Vaulstein May 19 '15 at 07:23
  • i assume the list that is the problem is `places`? We need to see the file it is being created from. – Abd Azrad May 19 '15 at 07:24
  • 1
    In `func_read` you assume that your 4 fields are seperated by `+` but in `func_order` you are not writing the `+` between the fields. The 4 fields are not seperated at all by any character. A `+` between two strings just concatenates them and does **not** add a `+` character between the strings – halex May 19 '15 at 07:26

1 Answers1

0

I guess, you are getting the error on either of these lines:

widgetsNum=places[1]
gidgetsNum=places[2]
doodadsNum=places[3]

The reason is some of the lines in your text file is not having three '+' symbols. Hence, on splitting, you are not having a list of 4 items. And hence, the error.

shruti1810
  • 3,920
  • 2
  • 16
  • 28