1

I have the following task:

  • We will write a program called ​student_register.py that allows students to register for an exam venue. 

  • First, ask the user how many students are registering. 

  • Create a ​for loop​ that runs for that amount of students 

  • Each loop asks for the student to enter their ID number. 

  • Write each of the ID numbers to a Text File called ​reg_form.txt 

  • This will be used as an attendance register that they will sign when they arrive at the exam venue. 

I think the following code I have is close as it saves the text file but only saves the first inputted ID number and does not display the others.

students = int(input("Please enter the amount of students registered."))
id_number = ""

for a in range (0, students):
    id_number = (int(input("Please enter student ID numbers.")))
    id_number = id_number + 1
    reg_form = open('reg_form.txt', 'w')
    reg_form.write("Student ID numbers: \n" + str(id_number))

print ("Thank you, ID numbers saved to txt file reg_form")
Community
  • 1
  • 1
  • `'w'` mode truncates the file if it already exists, i.e. it deletes the existing content. That's not the correct `open` mode (or the right place to open the file - you could do it once, *outside* the loop, for example) if that's not the behaviour you want. – jonrsharpe Jan 19 '20 at 12:18
  • use append mode `a` instead of `w` – zamir Jan 19 '20 at 12:20
  • move this line `reg_form = open('reg_form.txt', 'w')` to be BEFORE your loop, there is no reason to reopen the file over and over if you are just writing into the same file. it would also be better to use a context manager here so before you loop add the following line: `with open('reg_form.txt', 'w') as reg_form:`. also, `id_number = id_number + 1` was not in the instructions, why are you changing the id number entered by the student? – Nullman Jan 19 '20 at 12:25

2 Answers2

0

You are re-opening the file every loop, and you are opening it in write mode. This means you are overwriting any data in the file every loop.

A simple fix would be to open the file in append mode using 'a', alternatively you could store the results in a list and then write everything in one go at the end which would be nicer.

It's also good practise to close a file after you've finished with it, see Why Should I close files with Python. Whilst you can do this using reg_form.close() a better approach would be to use a context manager as this guarantees the file is always closed:

with open("reg_form.txt", "w") as my_file:
    my_file.write("some data")
Sam Broster
  • 542
  • 5
  • 15
0

It is not saving because you are not closing the file stream. I suggest that you use it with with keyword, it handles the opening and closing of your file and saving the data.

students = int(input("Please enter the amount of students registered."))
id_number = ""

for a in range (0, students):
    id_number = (int(input("Please enter student ID numbers.")))
    id_number = id_number + 1
    with open('reg_form.txt', 'w') as file:
        file.write("Student ID numbers: \n" + str(id_number))

print ("Thank you, ID numbers saved to txt file reg_form")
Radwan Abu-Odeh
  • 1,897
  • 9
  • 16