0

このPythonプログラムを完全に機能させることはできません。実行するたびに、25行目に「else」ループである「インデントでのタブとスペースの一貫性のない使用」エラーが発生します。私はこれをすべて無駄に修正するさまざまな方法を試したので、誰かが私の問題を説明できるかどうか疑問に思いました。どうもありがとうございました。

questions = ["What does RAD stand for?",
        "Why is RAD faster than other development methods?",
        "Name one of the 3 requirements for a user friendly system",
        "What is an efficient design?",
        "What is the definition of a validation method?"]


answers = ["A - Random Applications Development, B - Recently Available Diskspace, C - Rapid Applications Development",
        "A - Prototyping is faster than creating a finished product, B - Through the use of CASE tools, C - As end user evaluates before the dev team",
        "A - Efficient design, B - Intonated design, C - Aesthetic design",
        "A - One which makes best use of available facilities, B - One which allows the user to input data accurately, C - One which the end user is comfortable with",
        "A - A rejection of data which occurs because input breaks predetermined criteria, B - A double entry of data to ensure it is accurate, C - An adaption to cope with a change external to the system"]

correctanswers = ["C", "B", "A", "A", "A"]

score = 0

for i in range(len(questions)):
    a = 1
    print (questions[a])
    print (answers[a])
    useranswer = (input("Please enter the correct answer's letter here:"))
    correctanswer = correctanswers[a]
    if useranswer is correctanswer:
            print("Correct, well done!")
            score = score + 1
        else:
            print("Incorrect, sorry!")

print("Well done, you scored" + score + "//" + int(len(questions)))
4

4 に答える 4

3

タブとスペースを混在させています。Pythonを実行して次の-ttことを確認してください。

python -tt scriptname.py

インデントにスペースのみを使用するようにエディターを構成します。通常、すべてのタブをスペースに変換するメニューオプションもあります。

明らかに、コードをスタックオーバーフローウィンドウに貼り付けると、行がスペースのみを使用していることがわかりますelse:が、他の行はタブを使用しています。

    if useranswer is correctanswer:
            print("Correct, well done!")
            score = score + 1
        else:
            print("Incorrect, sorry!")

StackOverflowはタブを4つのスペースにレンダリングするためです。

于 2013-02-15T12:06:40.177 に答える
1

コードをインデントするときは、タブまたはスペースを使用してください。それらを混在させないでください(つまり、ある行でタブを使用し、次の行でスペースを使用することはできません)。

于 2013-02-15T12:06:17.837 に答える
1

の同じインデントelseレベルにある必要がありifます。

   if useranswer is correctanswer:
        print("Correct, well done!")
        score = score + 1
   else:
        print("Incorrect, sorry!")
于 2013-02-15T12:06:33.137 に答える
0

少なくともこのコードブロックでは、ifとelseは異なる意図であり、同じである必要があります。

if useranswer is correctanswer:
        print("Correct, well done!")
        score = score + 1
    else:
        print("Incorrect, sorry!")

する必要があります:

if useranswer is correctanswer:
        print("Correct, well done!")
        score = score + 1
else:
        print("Incorrect, sorry!")
于 2013-02-15T12:06:19.193 に答える