def function(score,name):
sumOfStudent = (name + ' scored ' + str(score))
f = open('test.txt', 'wb')
f.write(sumOfStudent)
f.close()
user_name = input("Please enter yout full name: ")
user_score = int(input("Please enter your score: "))
function(user_score,user_name)
f = open('test.txt')
print(f.read())
f.close()
我正在用python编写一个简单的程序,允许用户输入信息,然后将文本存储在一个.txt文件中。这工作,但它总是写在同一行,我想知道我会如何使f.write(sumOfStudent)
在一个新的行每次(sumOfStudent是变量,以容纳用户输入)谢谢!你知道吗
嘿,你要做的不是每次都写到你要覆盖的文件的末尾,你要做的是用“a”将它附加到文件中
另外,要写入新行,您必须通过声明新行“\n”来告诉程序您正在执行的操作
相关问题 更多 >
编程相关推荐