在从文本文件加载列表中找不到Python列表索引

在从文本文件加载列表中找不到Python列表索引,python,list,text-files,Python,List,Text Files,任务是让用户输入4个数字,然后将它们存储在一个文本文件中,打开该文本文件,在不同的行上显示4个数字,然后获得这些数字的平均值并将其显示给用户。 以下是我目前的代码: __author__ = 'Luca Sorrentino' numbers = open("Numbers", 'r+') numbers.truncate() #OPENS THE FILE AND DELETES THE PREVIOUS CONTENT # Otherwise it

任务是让用户输入4个数字,然后将它们存储在一个文本文件中,打开该文本文件,在不同的行上显示4个数字,然后获得这些数字的平均值并将其显示给用户。 以下是我目前的代码:

__author__ = 'Luca Sorrentino'


numbers = open("Numbers", 'r+')
numbers.truncate() #OPENS THE FILE AND DELETES THE PREVIOUS CONTENT
                    # Otherwise it prints out all the inputs into the file ever

numbers = open("Numbers", 'a')  #Opens the file so that it can be added to
liist = list() #Creates a list called liist

def entry(): #Defines a function called entry, to enable the user to enter numbers
        try:
            inputt = float(input("Please enter a number"))  #Stores the users input as a float in a variable
            liist.append(inputt) #Appends the input into liist
        except ValueError: #Error catching that loops until input is correct
            print("Please try again. Ensure your input is a valid number in numerical form")
            entry() #Runs entry function again to enable the user to retry.

x = 0
while x < 4:  # While loop so that the program collects 4 numbers
    entry()
    x = x + 1

for inputt in liist:
  numbers.write("%s\n" % inputt) #Writes liist into the text file


numbers.close() #Closes the file

numbers = open("Numbers", 'r+')

output = (numbers.readlines())

my_list = list()
my_list.append(output)

print(my_list)
print(my_list[1])
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
数字=打开(“数字”,“r+”)
numbers.truncate()#打开文件并删除以前的内容
#否则,它会将所有输入打印到文件中
numbers=open(“numbers”,a')#打开文件以便将其添加到
liist=list()#创建一个名为liist的列表
def entry():#定义一个名为entry的函数,使用户可以输入数字
尝试:
input=float(输入(“请输入一个数字”)#将用户输入作为浮点存储在变量中
liist.append(input)#将输入附加到liist中
ValueError除外:#在输入正确之前捕捉循环时出错
打印(“请重试。确保您的输入是数字形式的有效数字”)
entry()#再次运行entry函数以允许用户重试。
x=0
while x<4:#while循环,以便程序收集4个数字
条目()
x=x+1
对于liist中的输入:
number.write(“%s\n”%input)#将列表写入文本文件
number.close()#关闭文件
数字=打开(“数字”,“r+”)
输出=(numbers.readlines())
我的清单=清单()
my_list.append(输出)
打印(我的列表)
打印(我的列表[1])
问题是从文本文件中加载数字,然后将每个数字存储为一个变量,这样我就可以得到它们的平均值。 我似乎找不到一种方法来专门定位每个数字,只是每个字节,而不是我想要的。

您的列表(我的列表)只有一个项目-一个包含您想要的项目的列表

如果尝试打印(len(my_list)),您会看到这一点,因此您的打印(my_list[1])超出范围,因为索引为1的项不存在

当您创建一个空列表并附加输出时,您正在向列表中添加一个项目,这是变量输出为值保留的内容

想要得到你想要的,就去做吧

my_list = list(output)

您可以稍微更改程序的结尾,它将起作用:

output = numbers.readlines()
# this line uses a list comprehension to make 
# a new list without new lines
output = [i.strip() for i in output]
for num in output:
    print(num)
1.0
2.0
3.0
4.0

print sum(float(i) for i in output)
10

你会有两个主要问题

首先,
.append()
用于将单个项目添加到列表中,而不是将一个列表添加到另一个列表中。因为您使用了
.append()
,所以您得到了一个包含一个项目的列表,而该项目本身就是一个列表。。。不是您想要的,以及错误消息的解释。将一个列表连接到另一个列表时,
.extend()
+=
会起作用,但您应该问问自己,在您的情况下是否有必要这样做

其次,列表元素是字符串,您希望将它们作为数字使用
float()
将为您转换它们

一般来说,你应该研究“列表理解”的概念。这样的操作非常方便。下面的示例创建了一个新列表,其成员分别是
float()
ed版本的
.readlines()
输出:

my_list = [float(x) for x in output]
在列表理解中添加条件的能力也是一个真正的复杂性节约。例如,如果您想跳过文件中的任何空白/空白行:

my_list = [float(x) for x in output if len(x.strip())]

取出
读取行
函数周围的括号,然后尝试
打印(输出)
:您应该会看到您的数字列表。除了
打印(我的列表)
输出错误和
打印(我的列表[1])
崩溃之外,此代码还有一些其他问题。一旦你开始工作,我鼓励你问一个关于的问题。离题:你的文件管理真的很差。使用“r+”打开文件一次,只是为了截断(不是实际读取),然后(不关闭)重新打开以进行追加,然后重新打开以获取“r+”(这次是读取,但不是写入,因此+是没有意义的)。只需为“w+”打开一次,它将允许您读写,并为您截断文件。当您完成写入(填充文件)时,您可以
查找
返回到开始读取它。您也可以切换到
with
语句来管理文件对象的生存期。@ShadowRanger您好,谢谢。我已经用“w”替换了所有不同格式的开头,它的工作原理与我不确定如何让它回到开头的事实不同?谢谢,@lucafsorrentino:既然你既在读也在写(但想截断),你应该使用模式
w+
。用于查找的API是。