我在三种不同的方式尝试这个节目,我知道我接近了几次,但失败了这么多次我都放弃了,需要一组额外的眼睛之后。 我知道这个程序是“简单的”,但我知道我在想这件事。的Python 3.3 - 多选名单,从阅读和.txt然后
程序应该在列表中存储正确答案。使用该列表对20个问题测试进行评分。 然后阅读该student.txt文件,以确定学生如何回答。 阅读.txt文件后,它应该评分,然后显示通过或失败(通过= 15或更大) 它终于显示总数或正确的,不正确的答案与学生错过的问题列表。
下面是所有三次尝试。任何帮助是极大的赞赏。
3210
# This program stores the correct answer for a test
# then reads students answers from .txt file
# after reading determines and dislpays pass or fail (15 correct to pass)
# Displays number of correct and incorrect answers for student
# then displays the number for the missed question/s
#Creat the answer list
def main ():
# Create the answer key list
key = [ B, D, A, A, C, A, B, A, C, D, B, C, D, A, D, C, C, B, D, A,]
print (key)
# Read the contents of the student_answers.txt and insert them into a list
def read_student():
# Open file for reading
infile = open ('student_answers.txt', 'r')
# Read the contents of the file into a list
student = infile.readlines ()
# Close file
infile.close ()
# Strip the \n from each element
index = 0
while index < len(student):
student[index] = student[index].rstrip ('\n')
# Print the contents of the list
print (student)
# Determine pass or fail and display all results
def pass_fail(answers, student):
# Lists to be used to compile amount correct,
# amount wrong, and questions number missed
correct_list = []
wrong_list = []
missed_list = []
# For statement to compile lists
for ai,bi in zip (key,student):
if ai == bi:
correct_list.append(ai)
else:
wrong_list.append(ai)
missed_list.append(ai)
# Printing results for user
print(correct_list,' were answered correctly')
print(wrong_list,' questions were missed')
# If statement to determine pass or fail
if len(correct_list) >=15:
print('Congrats you have passed')
else:
print('Sorry you have faild please study and try, \
again in 90 days')
print('Any attempt to retake test before 90 days, \
will result in suspension of any licenses')
# Displaying the question number for the incorrect answer
print ('You missed questions number ', missed_list)
main()
a = (1, 'A'),(2,'C'),(3,'B')
b = (1,'A'), (2,'A'),(3,'C')
correct_list = []
wrong_list = []
missed_list = []
for ai, bi in zip (a, b):
if ai == bi:
correct_list.append(ai)
else:
wrong_list.append(ai)
missed_list.append(ai)
index(ai)+1
print(correct_list,'answered correct')
print(wrong_list, 'answered wrong')
if len(correct_list) >=2:
print ('Congrats you have passed')
else:
print ('Sorry you have faild please study and try again in 90 days')
print('Any attempt to retake test before 90 days will result in suspension of any lisences')
print ('Question number missed', missed_list)
什么不行? – Blender
如何编写txt文件?单行,逗号分隔,或每个答案的新行? –