0

次のエラーメッセージが表示されます:

Traceback (most recent call last):
 File "/Volumes/KINGSTON/Programming/Assignment.py", line 17, in <module>
    Assignment()
 File "/Volumes/KINGSTON/Programming/Assignment.py", line 3, in Assignment

私のコードは次のとおりです。

def Assignment():
    prompt = 'What is your PIN?'
    result = PIN
    error = 'Incorrect, please try again'
    retries = 2
    while result == PIN:
        ok = raw_input(Prompt)
        if ok == 1234:
            result = menu
        else:
            print error
            retries = retries - 1

        if retries < 0:
            print 'You have used your maximum number of attempts. Goodbye.'

Assignment():

誰かが私がどこで間違っているのかを知っていて、説明できるなら、少し助けていただければ幸いです

4

1 に答える 1

0

と言うとresult = PINPIN実際には存在しないため、その特定のエラーが発生します。引用符で囲まれていないため、Python は変数名であると想定しますが、その変数が何と等しいかを確認しようとすると、何も見つからず、NameError. それを修正すると、prompt後でPrompt.

これが完全なコードかどうかわからないので、他の問題が何であるかはわかりませんが、ループを制御するためにresultとを使用しているようです。ループは、チェックしている条件が満たされるまで(または手動で抜け出すまで) 実行されることに注意してください。したがって、追加の変数を宣言する代わりに、次のようなものから始めることができます。PINwhilewhileFalse

def Assignment():
    # No need to declare the other variables as they are only used once
    tries = 2

    # Go until tries == 0
    while tries > 0:
        ok = raw_input('What is your PIN?')
        # Remember that the output of `raw_input` is a string, so either make your
        # comparison value a string or your raw_input an int (here, 1234 is a string)
        if ok == '1234':
            # Here is another spot where you may hit an error if menu doesn't exist
            result = menu
            # Assuming that you can exit now, you use break
            break
        else:
            print 'Incorrect, please try again'
            # Little shortcut - you can rewrite tries = tries - 1 like this
            tries -= 1

        # I'll leave this for you to sort out, but do you want to show them both
        # the 'Please try again' and the 'Maximum attempts' messages?
        if tries == 0:
            print 'You have used your maximum number of attempts. Goodbye.'
于 2012-11-02T14:08:47.007 に答える