Sherwyn Mark Naidoo
Sherwyn Mark Naidoo

Reputation: 11

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

I have a loop that runs as many times as user inputs but I am unable to save all the inputs from the user into a text file

       ofile = open('RegForm.txt', 'a') 
  1. User inputs the number of time that the loop will run

       loops = int(input("How many students will be registering for the course? "))
    
       inputs = []
       for x in range(loops):
    
  2. Loop will run based on the user initial input requesting user to input addition info that will be save in a txt file but it only saves the last input. inputs.append(input("Enter the students number: "))

       ofile.write(inputs)
       ofile.close()
    

Upvotes: 1

Views: 138

Answers (1)

nagasantosh
nagasantosh

Reputation: 11

Is the objective to store the complete list to the file? In that case, you don't need to append each item in the list to the file during the for loop.

Look the code below:

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)

Upvotes: 1

Related Questions