繁体   English   中英

Python - 无法将用户的所有输入保存到文本文件中

[英]Python - unable to save all the inputs from the user into a text file

我有一个循环,运行次数与用户输入一样多,但我无法将用户的所有输入保存到文本文件中

       ofile = open('RegForm.txt', 'a') 
  1. 用户输入循环运行的次数

     loops = int(input("How many students will be registering for the course? ")) inputs = [] for x in range(loops):
  2. 循环将根据用户初始输入运行,要求用户输入将保存在 txt 文件中的附加信息,但它只保存最后一个输入。 input.append(input("请输入学生编号:"))

     ofile.write(inputs) ofile.close()

目标是将完整列表存储到文件中吗? 在这种情况下,您不需要在 for 循环期间将列表中的每个项目附加到文件中。

看看下面的代码:

loops = int(input("How many students will be registering for the course? "))

inputs = []

for x in range(loops):
    inputs.append(input("Enter the students number: "))

print(inputs)

with open('RegForm.txt', 'w') as ofile:

    for item in inputs:

        ofile.write("%i\n" % item)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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