2013-04-16 27 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) + "%")