Почему я не могу получить доступ к выбору 1 после использования его в цикле - PullRequest
3 голосов
/ 18 января 2020

Привет всем, в настоящее время я ищу помощь в своем коде. В первом l oop мой выбор 1 работает, но после процесса 1 возникнет вопрос, хотите ли вы продолжить или нет. Если я выберу «Нет» и снова введу выбор 1, l oop не примет его, но примет 2, 3 и 4. Спасибо за помощь, если кто-то захочет помочь.

student_id = 0
correct_answers = 0
passed_students = 0
failed_students = 0
isRunning = True
isTakingExam = True
isRetakeCorrect = True
class_record = []
students_exam_answer = []
answer_list = ['D', 'C', 'D', 'D', 'A',
               'B', 'C', 'A', 'B', 'B',
               'C', 'D', 'A', 'B', 'C',
               'D', 'D', 'A', 'B', 'A',
               'A', 'B', 'D', 'A', 'C']

try:
    while isRunning:
        print('\nExamination System v1.0\n\n'
              '[1] Take the exam\n'
              '[2] Check my exam\n'
              '[3] Display class record\n'
              '[4] Exit\n')

        service = int(input('Enter your choice: '))

        if 0 < service < 5:
            if service == 1:
                while isTakingExam:
                    # Storing the Students Name
                    student_name = input('Enter your full name: ')
                    students_exam_answer.append([student_name])

                    # Storing the Students Answer
                    for count in range(len(answer_list)):
                        student_answer = input(f'Enter Answer For {count + 1}: ')
                        students_exam_answer[student_id].append(student_answer.upper())

                    # Checking the Students Answer
                    for answers in range(len(answer_list)):
                        if students_exam_answer[student_id][answers + 1] == answer_list[answers]:
                            correct_answers += 1

                    # Checking if the Student passed or failed
                    if correct_answers >= 15:
                        print(f'\n{student_name} passed the exam. Score: {correct_answers} out of 25')
                        passed_students += 1
                    else:
                        print(f'\n{student_name} failed the exam. Score: {correct_answers} out of 25')
                        failed_students += 1

                    print(f'\nExam Status:\n'
                          f'Correct Answers: {correct_answers}\n'
                          f'Incorrect Answers: {25 - correct_answers}\n')

                    # Resetting variables that is needed in the next loop
                    class_record.append([student_name, correct_answers])
                    student_id += 1
                    correct_answers = 0
                    isRetakeCorrect = True
                    while isRetakeCorrect:
                        retake = input('\nNew Examinee? [Y/N]: ')
                        if retake.isalpha() and retake.upper() == 'Y':
                            isRetakeCorrect = False
                            isTakingExam = True

                        elif retake.isalpha() and retake.upper() == 'N':
                            isRetakeCorrect = False
                            isTakingExam = False
                        else:
                            print('\nInvalid input. Please try again.\n')

            elif service == 2:
                student_name = input('Enter your full name: ')
                for students in range(len(students_exam_answer)):
                    if student_name in students_exam_answer[students]:
                        print(f'\nScore: {class_record[students][1]}')
                        for answers in range(len(students_exam_answer[students]) - 1):
                            print(f'Number {answers + 1}: {students_exam_answer[students][answers + 1]} '
                                  f'(Correct Answer: {answer_list[answers]})')

                input('\nPress Enter key to continue...')

            elif service == 3:
                print(f'\nPassed Students: {passed_students}\n'
                      f'Failed Students: {failed_students}\n')
                if passed_students == 0 and failed_students == 0:
                    print('No record found.')
                else:
                    for record in class_record:
                        print(f'Name: {class_record[class_record.index(record)][0]}\n'
                              f'Score: {class_record[class_record.index(record)][1]}\n')

                input('\nPress Enter key to continue...')

            elif service == 4:
                exit(0)

        else:
            print('\nInvalid input. Please try again.\n')

except ValueError:
    print('\nInvalid input. Please try again.')

1 Ответ

3 голосов
/ 18 января 2020

Я думаю, что проблема связана с переменной isTakingExam, когда пользователь отвечает No, код устанавливает эту переменную на False, чтобы выйти из l oop, возможно, вам следует установить ее на True, просто до l oop:

...
if service == 1:
    isTakingExam=True
    while isTakingExam: 
    ...
...