Python 3,IDLE表示变量未定义,但确实如此

时间:2013-05-27 19:32:52

标签: python python-3.x

我正在创建一个输入密码的程序,您可以玩游戏。在我的一个定义riddle()中,它告诉我d1d2d3d4d5在被引用之前定义,但据我所知,他们已经定义。此外,当这仍然有效时,我试图让它解决一个任务会让它说它已经完成,但当我完成一个时,它仍然说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()

1 个答案:

答案 0 :(得分: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,而是 d中包含索引。您正在改变d,而不是将其替换为其他值。

其他评论;这一行:

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']。后者有效,但必须做两个的事情;遍历列表并测试每个元素的相等性。 ==直接进行平等测试。