1

ファイル「answer.txt」を開くプログラムに取り組んでいます。このファイルは、テストに対する学生の解答をシミュレートします。次に、このプログラムはファイルを answerKey と比較します。回答キーと学生の回答を 1 行ずつ出力します。良い回答と悪い回答の数を保持します。最後にスコアを出力します。このプログラムを 2 つの異なる回答キーで動作させることができますが、ファイルから回答を取得しようとすると、出力が多すぎます。ファイル内の最初の回答をスキップします。次に、回答キーから B、D のみを前後に出力します。生徒側は 2 番目の回答から印刷を開始し、1 つおきの回答をスキップします。

私のコード:

def main():
    try:
        answerKey = ['B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C',\
                 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A']
        index = 0
        numCorrect = 0
        answer_file = open('answers.txt', 'r')

        studentExam = answer_file.readline()

        print('Correct\tYour\tStatus\nAns.\tAns.\n-----------------------\n')
        while studentExam != "":
            problem_number = index + 1
            studentExam = studentExam.rstrip("\n")

            studentExam = answer_file.readline()

            for answerLine, studentLine in zip (answerKey, studentExam):
                answer = answerLine.split()
                studentAnswer = studentLine.split()

                if studentAnswer != answer:
                    print( 'You got that question number', index + 1, 'wrong\n the correct answer was' ,answer, 'but you answered' , studentAnswer)
                    index += 1
                else:
                    numCorrect += 1
                    index += 1

        grade = int((numCorrect / 20) * 100)

        print (' The number of correctly answered questions: ', numCorrect)

        print (' The number of incorrectly answered questions: ', 20 - numCorrect)

        print (' Your grade is', grade, '%')

        if grade <= 75:
                    print (' You have not passed ')
        else:
                    print (' Congrats you have passed ')
    except IOError:
        print("The file could not be found")
    except IndexError:
        print("There was an indexing error")
    except:
        print("An error occurred")
main()

私の出力:

You got that question number 1 wrong
 the correct answer was ['B'] but you answered ['D']

You got that question number 2 wrong
the correct  answer was ['D'] but you answered []

You got that question number 3 wrong
 the correct answer was ['B'] but you answered ['A']

You got that question number 4 wrong
 the correct answer was ['D'] but you answered []

You got that question number 5 wrong
 the correct answer was ['B'] but you answered ['A']

You got that question number 6 wrong
 the correct answer was ['D'] but you answered []

You got that question number 7 wrong
 the correct answer was ['B'] but you answered ['C']

You got that question number 8 wrong
 the correct answer was ['D'] but you answered []

You got that question number 9 wrong
 the correct answer was ['B'] but you answered ['A']

You got that question number 10 wrong
 the correct answer was ['D'] but you answered []

You got that question number 12 wrong
 the correct answer was ['D'] but you answered []

You got that question number 13 wrong
 the correct answer was ['B'] but you answered ['A']

You got that question number 14 wrong
 the correct answer was ['D'] but you answered []

You got that question number 15 wrong
 the correct answer was ['B'] but you answered ['C']

You got that question number 16 wrong
 the correct answer was ['D'] but you answered []

You got that question number 17 wrong
 the correct answer was ['B'] but you answered ['C']

You got that question number 18 wrong
 the correct answer was ['D'] but you answered []

You got that question number 19 wrong
 the correct answer was ['B'] but you answered ['C']

You got that question number 20 wrong
 the correct answer was ['D'] but you answered []

You got that question number 21 wrong
 the correct answer was ['B'] but you answered ['C']

You got that question number 22 wrong
 the correct answer was ['D'] but you answered []

You got that question number 23 wrong
 the correct answer was ['B'] but you answered ['D']

You got that question number 24 wrong
 the correct answer was ['D'] but you answered []

You got that question number 25 wrong
 the correct answer was ['B'] but you answered ['A']

You got that question number 26 wrong
 the correct answer was ['D'] but you answered []

You got that question number 27 wrong
 the correct answer was ['B'] but you answered ['D']

You got that question number 28 wrong
 the correct answer was ['D'] but you answered []

You got that question number 29 wrong
 the correct answer was ['B'] but you answered ['C']

You got that question number 30 wrong
 the correct answer was ['D'] but you answered []

You got that question number 31 wrong
 the correct answer was ['B'] but you answered ['C']

You got that question number 32 wrong
 the correct answer was ['D'] but you answered []

You got that question number 34 wrong
 the correct answer was ['D'] but you answered []

You got that question number 35 wrong
 the correct answer was ['B'] but you answered ['D']

You got that question number 36 wrong
 the correct answer was ['D'] but you answered []

You got that question number 37 wrong
 the correct answer was ['B'] but you answered ['D']

You got that question number 38 wrong
 the correct answer was ['D'] but you answered []

 The number of correctly answered questions:  2
 The number of incorrectly answered questions:  18
 Your grade is 10 %

正しいデータを比較していません。この問題の修正を手伝ってください。回答は 20 個しかなく、ファイル内の回答は次のようになります。

B
D
A
A
C
A
B
A
C
C
C
C
D
A
D
C
C
B
D
D

それぞれが独自の行にあります。そのため、ループを使用して回答キーの各回答を調べ、このファイルの各回答と比較しようとしています。私のpythonコードの何が問題なのか誰か教えてもらえますか?

4

2 に答える 2

0

あなたのアルゴリズムはあなたが期待するものではありません。を使用してファイルをループし、readlineそのファイルのすべての行に対して、回答キーをループしようとしています。ファイルの各行'B\n'は、グレードと改行文字の 2 文字 ( ) を返します。このfor answerLine, studentLine in zip (answerKey, studentExam):行を実行zipすると、2 要素の studentExam を使用して長い回答キーが生成されます (文字列は反復可能であることを思い出してください)。したがって、キーに回答する最初の人だけが表示されます (2 番目の回答には空白の学生の回答があります)。

私がお勧めするのは、アルゴリズムの最初に生徒の回答を一度にすべて読み込んでから、zipその完全なリストで使用することです。それはあなたが望むものをあなたに与えるでしょう。

PS、おそらくこれが必要です:

answer = answerLine.strip()
studentAnswer = studentLine.strip()

これの代わりに:

answer = answerLine.split()
studentAnswer = studentLine.split()
于 2013-04-25T21:46:20.060 に答える
0

ファイルを反復処理する代わりに、ファイルを一度にリストに読み込みます。次に、データ行をサニタイズする必要もあります。これは、次の方法で実現されます。

answers = open('answers.txt', 'r').readlines()
answers = [answer.strip() for answer in answers]

whileそのすべての作業が考慮されるため、これによりループが意味をなさないものになります。

それができたら、あとはそのリストを使用するだけです。

for answerLine, studentLine in zip(answerKey, answers):

これにより、次の出力が得られます (適切に配置されている場合)。

Correct    Your    Status
Ans.    Ans.
-----------------------

('You got that question number', 10, 'wrong\n the correct answer was', ['D'], 'but you answered', ['C'])
('You got that question number', 11, 'wrong\n the correct answer was', ['B'], 'but you answered', ['C'])
('You got that question number', 20, 'wrong\n the correct answer was', ['A'], 'but you answered', ['D'])
(' The number of correctly answered questions: ', 17)
(' The number of incorrectly answered questions: ', 3)
(' Your grade is', 85, '%')
Congrats you have passed

また、空のブロックは使用しないことを強くお勧めします。exceptこれにより、コードのデバッグが悪夢のようになります。なぜなら、何がどこで間違っているのかを特定できないからです。

于 2013-04-25T21:49:28.517 に答える