[英]Python - unable to save all the inputs from the user into a text file
我有一個循環,運行次數與用戶輸入一樣多,但我無法將用戶的所有輸入保存到文本文件中
ofile = open('RegForm.txt', 'a')
用戶輸入循環運行的次數
loops = int(input("How many students will be registering for the course? ")) inputs = [] for x in range(loops):
循環將根據用戶初始輸入運行,要求用戶輸入將保存在 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.