简体   繁体   中英

Getting a “ index out of range ” error

Just in the process of writing a python script for a task that I am doing.

The error lies in that it returns a " index out of range " error. The data is being pulled from a text file, then written into a list.( I'll post the content of the text file right above the code ) Index [0] works just fine, but anything above [0] produces an error. Not sure how to go about this one, cheers! ( sorry if the indenting is off )

Textfile content - warmup tops,25,10,beanies,10,10,socks,7,9

 __author__ = 'Nieck ten Broek'
 import easygui as eg
 import time
 eg.msgbox("Welcome to Ramu's stock app")
 stock_list = [ ]

 def show_clubmember():
    member_infile = open("members.txt","r")
    display_member = member_infile.read()
    print(display_member)

 def add_clubmember():
    member_name = eg.enterbox("Please enter your full name")
    member_number = eg.enterbox("please enter your preferred phone number")
    member_outfile = open("members.txt","a")
    member_outfile.write(member_name + ",")
    member_outfile.write(member_number + ",")
    member_outfile.write(time.strftime("%c")+ '\n')
    member_outfile.close()

def read_stock():
    stock_outifle = open("stock.txt","r")
    read_stock = stock_outifle.read()
    print(read_stock)
    stock_list.append(read_stock.strip("\n"))
    print(stock_list)

def change_stock():
    change_stock_outfile = open("stock.txt","a")

def sale():
    sale_stock = open("stock.txt","r")
    read_sale_stock = sale_stock.read()
    print(read_sale_stock)
    stock_list.write(read_sale_stock)
    warm_cost = stock_list[0]
    beanie_cost = stock_list[4]
    sock_cost = stock_list[8]
    print(stock_list)

    #print(stock_list)
    #eg.msgbox("what would you like to buy?")
    # choices = ("warmup tops","beanies","socks")
    #eg.choicebox("select options","selection",(choices))

def main():
    menu_options=("Add member","Read stock","Change stock","sale",
    "show members","quit")
    choice = eg.choicebox("menu","menu",menu_options)
    print(choice)
    #choice = input("Please select your option")
    if choice == "Add member":
        add_clubmember()
        main()
    elif choice == "Read stock":
        read_stock()
        main()

    elif choice == "Change stock":
        change_stock()
        main()

    elif choice =="Sale":
        sale()
        main()

    elif choice == "show members":
        show_clubmember()
        main()
    elif choice == "quit":
        quit()

    else:
        quit()
main()

I assume this is actually your read_stock function with proper indenting:

def read_stock():
    stock_outifle = open("stock.txt","r")
    read_stock = stock_outifle.read()
    print(read_stock)
    stock_list.append(read_stock.strip("\n"))
    print(stock_list)

You are reading the entire contents of the file into a single string, and appending that string to the stock_list. Since you append only one thing, of course your list will have only one element in it.

You want something like this:

def read_stock():
    stock_outfile = open("stock.txt","r")
    for line in stock_outfile:
        stock_list.append(line.strip())
    stock_outfile.close()

that will append each line of the file to the list. Probably you also need to split each line into its component fields and make that into some sort of object.

Your read_stock() function appears to read in all of the data as a single element. Right now after this function is called the lenght of your stock_list is one element (the entire line "warmup tops,25,10,beanies,10,10,socks,7,9)). What I imagine you are trying to is split the array by the commas.

   def original_read_stock():
      stock_outifle = open("stock.txt","r")
      read_stock = stock_outifle.read()
      print(read_stock)
      stock_list.append(read_stock.strip("\n"))
      print(stock_list)

To split the "stock" by commas, the following read method will do the trick:

   def read_stock_with_split():
      stock_outifle = open("stock.txt","r")
      read_stock = stock_outifle.read()
      print(read_stock)
      stock_list = read_stock.strip("\n").split(',')
      print(stock_list)

Notice that I have added a .split(',') to the the read stock text. This will create a list where each element is split at the comma as you desire. This will work as expected as long as you do not use newlines in between the text of your textfile.

when you read the file you must split the test by ',' to convert it into List then you can access to the index, if you dont do this you just have a index[0]

def sale():
    sale_stock = open("stock.txt","r")
    read_sale_stock = sale_stock.read()
    print(read_sale_stock)
    stock_list.write(read_sale_stock)
    stock_list = stock_list.split(',')  <-- change
    warm_cost = stock_list[0]
    beanie_cost = stock_list[4]
    sock_cost = stock_list[8]
    print(stock_list)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM