The second extra challenge on the modify task, and the hardest version of the program. The editor starts you off with the same student program as before.
Build the teacher and student program again, with the grade on the same line as the student’s name, exactly as in the previous exercise:
teacher or student.TeacherInfo.txt, each on a line of its own.StudentInfo.txt: the name, a space, and the grade.This time the program also validates the answer to the question about
another person. It only accepts yes or no, in lower case:
Enter another person? yes/no.y, YES, No or an empty line included, is not
accepted. The program prints Please answer yes or no, asks the question
again, and reads a new answer. It keeps doing that until the answer is
yes or no.yes goes round the loop again. no ends it, and the program prints
Goodbye!.There is more than one way to check the answer. You can join two comparisons
with and, or check whether the answer is in a list of the answers you
accept, as in the chapter on lists. Either way, the loop that does the checking
goes inside the loop that is already there.
Need a hint about the second way? Click here.
Each repetition starts with these questions, as before:
Enter teacher or student
Enter teacher name
Enter teacher code
Enter student name
Enter student grade
Enter teacher or student comes first, then either the two teacher questions or
the two student questions. Then the question about another person:
Enter another person? yes/no
After an answer that is not accepted, these two lines, once for every such answer:
Please answer yes or no
Enter another person? yes/no
And after no, the last line:
Goodbye!
StudentInfo.txt and TeacherInfo.txt might start like this:
Leo 6
Mr Price
JPR
Input:
student
Ben
5
YES
yes
teacher
Mr Hughes
HUG
No
no
Output:
Enter teacher or student
Enter student name
Enter student grade
Enter another person? yes/no
Please answer yes or no
Enter another person? yes/no
Enter teacher or student
Enter teacher name
Enter teacher code
Enter another person? yes/no
Please answer yes or no
Enter another person? yes/no
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 space between a name and a grade and the "\n" at the end of every
line.
Make sure that you check for the following things:
"yes" and "no" exactly: YES, Yes and y
are all different pieces of text.yes or no needs both comparisons, so a
while that keeps asking needs choice != "yes" and choice != "no".Please answer yes or no, the question is printed again and a new
answer is read. Forget the input() and the checking loop never ends.if that
writes to the files.no sets the flag to False.