获得“索引超出范围”错误

时间:2015-09-04 01:47:18

标签: python indexing range

正在为我正在做的任务编写python脚本。

错误在于它返回“索引超出范围”错误。数据从文本文件中提取,然后写入列表。(我将在代码上方发布文本文件的内容)索引[0]工作得很好,但是[0]以上的任何内容都会产生错误。不知道如何去做这个,干杯! (对不起,如果缩进了)

文本文件内容 - 热身上衣,25,10,帽子,10,10,袜子,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()

3 个答案:

答案 0 :(得分:1)

我认为这实际上是你的read_stock函数,并且有适当的缩进:

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)

您正在将文件的全部内容读入单个字符串,并将该字符串附加到stock_list。由于你只附加了一件事,当然你的列表中只有一个元素。

你想要这样的东西:

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

将文件的每一行附加到列表中。您可能还需要将每一行拆分为其组件字段,并将其转换为某种对象。

答案 1 :(得分:1)

您的read_stock()函数似乎将所有数据作为单个元素读入。现在,在调用此函数后,stock_list的长度是一个元素(整行"热身顶部,25,10,帽子,10,10,袜子,7,9))。我想你想要的是用逗号分割数组。

   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)

分割" stock"通过逗号,以下读取方法将起到作用:

   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)

请注意,我已将.split(',')添加到读取的库存文本中。这将创建一个列表,其中每个元素都可以根据需要以逗号分隔。只要不在文本文件的文本之间使用换行符,这将按预期工作。

答案 2 :(得分:0)

当你读取文件时,你必须将测试分成','将其转换为List然后你可以访问索引,如果你不这样做,你只需要一个索引[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)