1

私はまだ多肢選択式クイズ プログラムに取り組んでいます。私はもうすぐそこにいます。まだいくつかのバグがあります。プログラムは、正解、不正解、および現在のパーセンテージの質問数を継続的に集計します。ただし、最初はユーザーが質問に答えていないため、プログラムは実行されません。したがって、NULL % は正しいです。私のコードは以下の通りです:

import random
import sys
import os
import math

right_answer_total = float(0)
wrong_answer_total = float(0)
answer_total = float(right_answer_total + wrong_answer_total)
percentage = 100 * (float(right_answer_total) / float(answer_total))

word_drills = {'class': 'Tell Python to make a new kind of thing.',
               'object': 'Two meanings: the most basic kind of thing, and any instance of some thing.',
               'instance': 'What you get when you tell Python to create a class.',
               'def': 'How you define a function inside a class.',
               'self': 'Inside the functions in a class, self is a variable for the instance/object being accessed.',
               'inheritance': 'The concept that one class can inherit traits from another class, much like you and your parents.',
               'composition': 'The concept that a class can be composed of other classes as parts, much like how a car has wheels.',
               'attribute': 'A property classes have that are from composition and are usually variables.',
               'is-a': 'A phrase to say that something inherits from another, as in a Salmon *** Fish',
               'has-a': 'A phrase to say that something is composed of other things or has a trait, as in a Salmon *** mouth.'}


def start():
    # For loop that creates a list named keys. It grabs 3 random keys from the dictionary word_drills
    keys = [x for x in random.sample(word_drills, 3)]
    # User is presented with a question. A value from the previous randomly selected keys is selected as the 'question'
    correctanswer = word_drills[random.choice(keys)]
    print "Question: ", correctanswer 
    # Set the variables key1, key2, & key3 to the 3 keys in the list 'keys'
    key1, key2, key3 = keys[0], keys[1], keys[2]
    # User is presented with 3 choices.
    print "\n\n(a)%s   (b)%s   (c)%s" % (key1, key2, key3)
    a, b, c = word_drills[key1], word_drills[key2], word_drills[key3]
    selection = raw_input("> ")
    print selection
    if selection == "a":
        if a == correctanswer:
            print "That's correct!"
            answered_correctly()
        else:
            print "I'm sorry, that is incorrect..."
            not_answered_correctly()
    elif selection == "b":
        if b == correctanswer:
            print "That's correct!"
            answered_correctly()
        else:
            print "I'm sorry, that is incorrect..."
            not_answered_correctly()
    elif selection == "c":
        if c == correctanswer:
            print "That's correct!"
            answered_correctly()
        else:
            print "I'm sorry, that is incorrect..."
            not_answered_correctly()
    else:
        print "That is not a valid selection."
        exit(0)

def answered_correctly():
    global right_answer_total
    right_answer_total += 1
    stat_tracking()

def not_answered_correctly():
    global wrong_answer_total
    wrong_answer_total += 1
    stat_tracking()

def stat_tracking():
    os.system('cls' if os.name=='nt' else 'clear') 
    print "-" * 37
    print "|         Stat Tracking             |"
    print "-" * 37
    print "| Correct | Incorrect |  Percentage  |"
    print "-" * 37
    print "|    %d    |     %d     |     %d %%     |" % (right_answer_total, wrong_answer_total, percentage) 
    print "-" * 37
    print "\n\n\n"
    start()

stat_tracking()

これに対する回避策があるかどうか、またはすべて間違っている可能性があるかどうかはわかりません。あらゆる支援をいただければ幸いです。ありがとう。

4

3 に答える 3

4

質問への回答がない場合、印刷しても意味がありません。除数が 0 の場合に特別なチェックを追加し、100% を出力するか、単に N/A などを出力します。

また、コードのヒントをいくつか紹介します。

right_answer_total = float(0)
wrong_answer_total = float(0)
answer_total = float(right_answer_total + wrong_answer_total)
percentage = 100 * (float(right_answer_total) / float(answer_total))

コードの先頭に置くfrom __future__ import divisionと、これらすべてのfloats は不要になります。

print "|    %d    |     %d     |     %d %%     |" % (right_answer_total, wrong_answer_total, percentage) 

文字列補間演算子は非推奨です。str.formatPython 2.5 との互換性が不要な場合は、代わりに使用してください。

print "|    {}    |     {}     |     {} %     |".format(right_answer_total, wrong_answer_total, percentage) 
于 2012-08-19T19:24:30.703 に答える
3

起動時にユーザーはまだ質問に答えていないため、最初に に設定するだけで済みます0

後で問題が発生した場合は、このようにゼロ除算を事前に確認できます

if answer_total != 0:
    percentage = 100 * (float(right_answer_total) / float(answer_total))
else:
    percentage = 0.0

関数を使用して int を変換する0.0代わりに、float リテラル ( など) を使用して変数を初期化することも検討してください。float

まだ見つかっている可能性のあるバグを修正したら、コードを持ってhttp://codereview.stackexchange.comにアクセスして、一般的なコーディングのアドバイスを得るのが良いでしょう。

于 2012-08-19T19:26:22.793 に答える
1

9 行目を次のように変更します。

percentage = 100 * (float(right_answer_total) / float(answer_total)) if (right_answer_total>0 and answer_total >0) else 0
于 2012-08-19T19:31:07.507 に答える