0

パスワードを入力するとゲームができるプログラムを作っています。私の定義の 1riddle()つではd1d2d3d4d5が定義される前に参照されていることがわかりますが、私の知る限り、それらは既に定義されています。また、これがまだ機能しているときに、タスクを解決すると完了したと言われるようにしようとしましたが、1つを完了しても、1は未完了などと表示されました. これらの問題の両方を修正する必要があります。

def riddle():
    d1 = 'n'
    d2 = 'n'
    d3 = 'n'
    d4 = 'n'
    d5 = 'n'
    def compcheck():
        print('There are 5 tasks to complete. Enter a number to see task.')
        if d1 in ('y'):
            t1 = 'Completed.'
        if d2 in ('y'):
            t2 = 'Completed.'
        if d3 in ('y'):
            t3 = 'Completed.'
        if d4 in ('y'):
            t4 = 'Completed.'
        if d5 in ('y'):
            t5 = 'Completed.'
        if d1 in ('n'):
            t1 = 'Incomplete.'
        if d2 in ('n'):
            t2 = 'Incomplete.'
        if d3 in ('n'):
            t3 = 'Incomplete.'
        if d4 in ('n'):
            t4 = 'Incomplete.'
        if d5 in ('n'):
            t5 = 'Incomplete.'
        print ('1 is ' + t1)
        print ('2 is ' + t2)
        print ('3 is ' + t3)
        print ('4 is ' + t4)
        print ('5 is ' + t5)
    def solve():
        compcheck()
        if d1 and d2 and d3 and d4 and d5 in ['y']:
            print ('The password is 10X2ID 4TK56N H87Y8G.')
        tasknumber = input().lower()
        if tasknumber in ('1'):
            print('Fill in the blanks: P_tho_ i_ a c_d_ng lan_u_ge. (No spaces. Ex: ldkjfonv)')
            task1ans = input().lower()
            if task1ans in ['ysoinga']:
                d1 = 'y'
            solve()
        if tasknumber in ('2'):
            print('Is the shape of a strand of DNA: A): a Lemniscate, B): a Hyperboloid, C): a Double Helix, or D): a Gömböc.')
            task2ans = input().lower()
            if task2ans in ['c']:
                d2 = 'y'
            solve()
        if tasknumber in ('3'):
            print ('What is the OS with a penguin mascot?')
            task3ans = input().lower()
            if task3ans in ('linux'):
                d3 = 'y'
            solve()
        if tasknumber in ('4'):
            print('')
        if tasknumber in ('5'):
            print('')
    solve()
4

1 に答える 1

6

solve関数内では、、 などの変数に代入しています。これにより、これらの変数はその関数に対してローカルになりますが、最初にその内容をテストしようとします。これがあなたのエラーの原因です。d1d2

これらの変数を宣言する必要がありますnonlocal:

def solve():
    nonlocal d1, d2, d3, d4, d5

代わりにリストを使用することもできます:

d = ['n'] * 5
t = ['Incomplete' if x == 'n' else 'Complete' for x in d]
for i, x in enumerate(t, 1):
    print('{} is {}'.format(i, x)

if tasknumber == '1':
    print('Fill in the blanks: P_tho_ i_ a c_d_ng lan_u_ge. (No spaces. Ex: ldkjfonv)')
    answer = input().lower()
    if answer == 'ysoinga':
        d[0] = 'y'
    solve()

nonlocalこれには、キーワードも不要になるという追加の利点があります。dではなく、 に含まれるインデックスに代入します dd別の値に置き換えるのではなく、 を変更しています。

その他の注意事項; この線:

if d1 and d2 and d3 and d4 and d5 in ['y']:

も機能しません。あなたはそれが次のことを意味していたと思います:

if d1 == 'y' and d2 == 'y' and d3 == 'y' and d4 == 'y' and d5 == 'y':

しかし、リストは次のようになります。

if all(x == 'y' for x in d):

多分

if d == ['y'] * 5:

特定の文字列をテストするときは== 'value to test for'、 ではなくを使用しますin ['value to test for']。後者は機能しますが、2 つのことを行う必要があります。リストをループし、各要素が等しいかどうかをテストします。==平等テストに直行します。

于 2013-05-27T19:35:49.443 に答える