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!")
Change the program so that it can store teachers as well as students, each in a file of their own.
TeacherInfo.txt.teacher or student, and the
program uses the answer to open the correct file.ACO), and appends them to TeacherInfo.txt.StudentInfo.txt.The user will only ever answer teacher or student to the first question, so
you do not need to handle any other answer.
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!.
StudentInfo.txt and TeacherInfo.txt might start like this:
Leo
6
Mr Price
JPR
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.
Make sure that you check for the following things:
"teacher" and
"student", spelled exactly like that, in lower case.TeacherInfo.txt and the student’s data to
StudentInfo.txt, both spelled exactly like that."a" mode, never "w"."\n".if, whichever kind of person was entered.