Drop links or images here to add them to the editor.

In a modify task you take code that already works and adapt it so that it does something else.

The editor starts you off with the program from the investigate task. It adds students to the end of StudentInfo.txt until the user answers n:

repeat = True

while repeat == True:
    print("Enter student name")
    student_name = input()
    student_name = student_name + "\n"

    data_file = open("StudentInfo.txt", "a")
    data_file.write(student_name)
    data_file.close()

    print("Enter another student? y/n")
    choice = input()

    if choice == "n":
        repeat = False
        print("Goodbye!")

Your task

Change the program so that it can store teachers as well as students, each in a file of their own.

The user will only ever answer teacher or student to the first question, so you do not need to handle any other answer.

The output

Each repetition starts with this question:

Enter teacher or student

For a teacher, the next two questions are:

Enter teacher name
Enter teacher code

For a student, they are:

Enter student name
Enter student grade

Now that the program handles teachers too, the question at the end of every repetition asks about a person rather than a student:

Enter another person? y/n

As before, only n ends the loop, and the program then prints Goodbye!.

The files

StudentInfo.txt and TeacherInfo.txt might start like this:

Leo
6
Mr Price
JPR

Example

Input:

student
Ben
5
y
teacher
Mr Hughes
HUG
n

Output:

Enter teacher or student
Enter student name
Enter student grade
Enter another person? y/n
Enter teacher or student
Enter teacher name
Enter teacher code
Enter another person? y/n
Goodbye!

StudentInfo.txt afterwards:

Leo
6
Ben
5

TeacherInfo.txt afterwards:

Mr Price
JPR
Mr Hughes
HUG

Ask each question with its own print, and read the answer with a bare input() on the next line, rather than passing the question to input(...). The questions are part of the output that is checked here, so their spelling, capital letters and punctuation count. So do the contents of both files, down to the "\n" at the end of every line.

Help! My code doesn't work!

Make sure that you check for the following things:

  • The answer to the first question is compared with "teacher" and "student", spelled exactly like that, in lower case.
  • The teacher’s data goes to TeacherInfo.txt and the student’s data to StudentInfo.txt, both spelled exactly like that.
  • Both files are opened in "a" mode, never "w".
  • Every value you write ends with "\n".
  • Every file you open is closed again inside the same branch.
  • The question about another person is asked once per repetition, after the if, whichever kind of person was entered.