Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.5-生成文本文件_Python_Loops_File Io - Fatal编程技术网

Python 3.5-生成文本文件

Python 3.5-生成文本文件,python,loops,file-io,Python,Loops,File Io,我必须编写一个程序,提示用户输入六个测试名称及其分数,并将其写入名为tests.txt的文本文件。我必须使用循环,每个输入都应该写入文件中自己的行。完成后,程序应生成确认消息 样本输出 Entering six tests and scores Enter test name objects Enter % score on this test 88 Enter test name loops Enter % score on this test 95 Enter test name sel

我必须编写一个程序,提示用户输入六个测试名称及其分数,并将其写入名为tests.txt的文本文件。我必须使用循环,每个输入都应该写入文件中自己的行。完成后,程序应生成确认消息

样本输出

Entering six tests and scores
Enter test name objects 
Enter % score on this test 88
Enter test name loops 
Enter % score on this test 95
Enter test name selections.   
Enter % score on this test 86 
Enter test name variables
Enter % score on this test 82
Enter test name files
Enter % score on this test 100
Enter test name functions
Enter % score on this test 80
File was created successfully
Reading six tests and scores
TEST          SCORE
objects       88
loops         95
selections    86
variables     82
files         100
functions     80

Average is 88.5
然后我必须编写另一个程序,使用循环读取和处理前一个程序中的tests.txt。程序必须输出一个显示测试名称和分数的两列表格。表格完成后,平均值应精确到小数点后一位

样本输出

Entering six tests and scores
Enter test name objects 
Enter % score on this test 88
Enter test name loops 
Enter % score on this test 95
Enter test name selections.   
Enter % score on this test 86 
Enter test name variables
Enter % score on this test 82
Enter test name files
Enter % score on this test 100
Enter test name functions
Enter % score on this test 80
File was created successfully
Reading six tests and scores
TEST          SCORE
objects       88
loops         95
selections    86
variables     82
files         100
functions     80

Average is 88.5
这远远超出了我必须做的任何事情。到目前为止,我的任务相当简单。我使用的是Python3.5,所以我不能使用任何新函数

随进度编辑

到目前为止,我为第一个生成txt文档的程序准备了以下内容:

def main():
myfile = open('test.txt', 'w')
test = input('Please enter test name or enter to quit ')

while test != '':
    score = int(input('Enter % score on this test '))
    myfile.write(test + '\n')
    myfile.write(str(score) + '\n')
    test = input('Please enter test name or enter to quit ')

myfile.close()
print('File was created successfully')
main()

第二个程序如下所示,形状粗糙:

def main():
f = open('test.txt', 'r'); 
text = f.read(); 
f.close()
main()

有人能给我指点方向吗?谢谢

第一部分 您可以这样写入文件:

with open('/path/to/your/file.name', 'w') as f:
    f.write('text')
如果文件不存在,将创建该文件并覆盖所有内容。如果将
'w'
替换为
'a'
,它将附加到文件末尾(您需要自己添加换行符)

如果在程序开始时执行
f=open('/path/to/your/file.name',w')
,然后在程序结束时执行
f.close()
,则将写入其间的所有内容(也就是说,在刷新并关闭输出流之前,它不会覆盖以前的
写入
操作)

例如:

f = open('file.txt', 'w')
f.write('hello, ')
f.write('world!\n')
f.close()
file.txt的内容设置为
hello,world

第二部分 列表的平均值是其元素之和除以元素数。字面上就是
lambda数组:sum(数组)/len(数组)

第三部分 要读取文件,您可以执行
f=open('file.txt','r');text=f.read();f、 关闭()
readline
读取一行而不是整个文件


从现在开始,编写实际的程序本身应该是相当简单的,因此我将把实际的编码留给您,因为这对我来说像是一个家庭作业问题。

f通常是什么意思?我以前从未使用过它。@GoldDial
f
只是一个变量名。。。这里我使用
f
来表示
文件(或者更确切地说,是
\u io.TextIOWrapper