簡體   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