gpt4 book ai didi

python-3.x - 为什么在循环中使用后无法访问选择 1

转载 作者:行者123 更新时间:2023-12-04 01:12:55 25 4
gpt4 key购买 nike

大家好,我目前正在寻求代码方面的帮助。在第一个循环中,我的选择 1 有效,但在 1 的过程之后,是否要继续会有一个问题。如果我选择“否”并再次输入选择 1,循环将不会接受它,但会接受 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.')

最佳答案

我认为问题与变量 isTakingExam 有关,当用户回答 No 时,代码将此变量设置为 False 以退出循环,也许你应该在循环之前将它设置为 True:

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

关于python-3.x - 为什么在循环中使用后无法访问选择 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59800756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com