2013-04-16 28 views
1

我在三种不同的方式尝试这个节目,我知道我接近了几次,但失败了这么多次我都放弃了,需要一组额外的眼睛之后。 我知道这个程序是“简单的”,但我知道我在想这件事。的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) 
+0

什么不行? – Blender

+0

如何编写txt文件?单行,逗号分隔,或每个答案的新行? –

回答

2

所以,我决定了你的第二个例子的工作,因为这是最简单的,我搞不懂。

这是一个修改后的文件,附有说明,它可以做我认为你想要的。我假设学生的答案是在一个名为student_answers.txt的文本文件中,并且每行有一个答案。

#!/usr/bin/env python 

def read_student(path): 
    with open(path, 'r') as infile: 
     contents = infile.read() 

    # Automatically gets rid of the newlines. 
    return contents.split('\n') 

def pass_fail(answers, student): 
    correct_list = [] 
    wrong_list = [] 

    # Use the 'enumerate' function to return the index. 
    for index, (ai, bi) in enumerate(zip(answers, student)): 
     # since problems start with 1, not 0 
     problem_number = index + 1 

     if ai == bi: 
      correct_list.append(problem_number) 
     else: 
      wrong_list.append(problem_number) 

    # Print the length of the list, not the list itself. 
    print(len(correct_list), 'were answered correctly') 
    print(len(wrong_list), 'questions were missed') 

    if len(correct_list) >= 15: 
     print('Congrats, you have passed') 
    else: 
     print('Sorry, you have failed. Please study and try again in 90 days') 
     print('Any attempt to retake test before 90 days will result in suspension of any licenses') 

    # Display each missed number on a separate line 
    print ('You missed questions numbers:') 
    for num in wrong_list: 
     print(' ' + str(num)) 



def main(): 
    key = ['B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 
     'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'] 
    student_answers = read_student('student_answers.txt') 
    pass_fail(key, student_answers) 

if __name__ == '__main__': 
    main() 

一些一般性的评论:

  • 在你main功能,您创建了一个键,打印它,并试图访问它pass_fail函数内。这是行不通的 - 当你在一个函数中创建一个变量时,它不能在函数之外自动访问。有几种解决方案可以完成:
    • 做成key一个全局变量。这是一个undesirable solution
    • 传递key可变进pass_fail功能。这就是我所做的。
  • 您读取学生文件的代码有点太复杂。您可以使用with语句,它会自动打开并关闭文件对象(infile)。同样,Python也有内置的方法来帮助分割文本文件中的字符串并删除换行符。此外,对于未来,你也可以遍历在像文件中的每个线之下,而不是通过手动将其循环:

    with open('file.txt') as my_file: 
        for line in my_file: 
         print(line) 
         # The newline is still a part of the string! 
    
  • 我不知道为什么你不得不里面wrong_listmissed_list两个变量pass_fail。我删除了missed_list

  • 您的for循环的问题在于您存储的是正确答案,而不是问题编号。为了解决这个问题,我使用了内置的enumerate函数,这对于这类事情非常有用。或者,您可以使用一个变量并在每个循环中增加一次。
  • print语句都有些畸形,所以我清理,截至
  • 打印时用户的结果,你不希望打印的清单。相反,您要打印列表的长度
  • 我不知道这是否是有意的,但你的钥匙没有字符串,而只有字母的名字。
0

除了Python将A,B,C和D解释为变量而不是字符之外,您的第一个答案非常接近。您在for循环中放置了评分逻辑。改变阵列是字符,而不是变量后,我得到了以下结果:

The number of correctly answered questions: 1 
The number of incorrectly answered questions: 19 
Your grade is 5 % 
You have not passed 
The number of correctly answered questions: 2 
The number of incorrectly answered questions: 18 
Your grade is 10 % 
You have not passed 
The number of correctly answered questions: 3 
The number of incorrectly answered questions: 17 
Your grade is 15 % 
You have not passed 
You got that question number 4 wrong 
the correct,  answer was ['A'] but you answered ['D'] 
The number of correctly answered questions: 3 
The number of incorrectly answered questions: 17 
Your grade is 15 % 
You have not passed 
The number of correctly answered questions: 4 
The number of incorrectly answered questions: 16 
Your grade is 20 % 
You have not passed 
The number of correctly answered questions: 5 
The number of incorrectly answered questions: 15 
Your grade is 25 % 
You have not passed 
The number of correctly answered questions: 6 
The number of incorrectly answered questions: 14 
Your grade is 30 % 
You have not passed 
The number of correctly answered questions: 7 
The number of incorrectly answered questions: 13 
Your grade is 35 % 
You have not passed 
The number of correctly answered questions: 8 
The number of incorrectly answered questions: 12 
Your grade is 40 % 
You have not passed 
You got that question number 10 wrong 
the correct,  answer was ['D'] but you answered ['A'] 
The number of correctly answered questions: 8 
The number of incorrectly answered questions: 12 
Your grade is 40 % 
You have not passed 
The number of correctly answered questions: 9 
The number of incorrectly answered questions: 11 
Your grade is 45 % 
You have not passed 
The number of correctly answered questions: 10 
The number of incorrectly answered questions: 10 
Your grade is 50 % 
You have not passed 
The number of correctly answered questions: 11 
The number of incorrectly answered questions: 9 
Your grade is 55 % 
You have not passed 
The number of correctly answered questions: 12 
The number of incorrectly answered questions: 8 
Your grade is 60 % 
You have not passed 
The number of correctly answered questions: 13 
The number of incorrectly answered questions: 7 
Your grade is 65 % 
You have not passed 
The number of correctly answered questions: 14 
The number of incorrectly answered questions: 6 
Your grade is 70 % 
You have not passed 
The number of correctly answered questions: 15 
The number of incorrectly answered questions: 5 
Your grade is 75 % 
You have not passed 
The number of correctly answered questions: 16 
The number of incorrectly answered questions: 4 
Your grade is 80 % 
Congrats you have passed 
The number of correctly answered questions: 17 
The number of incorrectly answered questions: 3 
Your grade is 85 % 
Congrats you have passed 
The number of correctly answered questions: 18 
The number of incorrectly answered questions: 2 
Your grade is 90 % 
Congrats you have passed 
Traceback (most recent call last): 
    File "a1.py", line 39, in <module> 
    answerKey.close() 
AttributeError: 'list' object has no attribute 'close' 

最后的错误是因为你已经关闭了这可能是在一个点一个文件对象数组,但你已经很接近。只是一个小的语法错误。

0

短版本,但没有工作。

correct = ['B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'] 
file_handler = open('student_answers.txt','r') 
answers = file_handler.readlines() 
valid = 0 
for element in enumerate(correct): 
    if answers[element[0]].rstrip("\n") == element[1]: 
     valid += 1 
if valid >= 15: 
    print("Congrats, you passed with " + str((valid/20)*100) + "%") 
else: 
    print("Sorry, you failed with " + str((valid/20)*100) + "%")