简体   繁体   中英

Local variable 'put' might be referenced before assignment

When I execute this code I'm not getting output and it says local variable might be referenced before assignment. I don't know what it is exactly.

def funct():
    try:
        no = 2
        put = []
        if no == 1:
            out = 'yes'
            put.append(out)
        elif no == 2:
            out = 'no'
            put.append(out)
        elif no == 3:
            out = 'cant predict'
            put.append(out)
    except ValueError:
        print("Something is wrong")
    else:
        print("Everything is ok")

    put = str(put)[1:-1]
    print(put)
    return put

PyCharm 警告消息的屏幕截图

Here is a similar question and the answer to it might help you, too:

Local variable might be referenced before assignment - Python

The issue addressed here was that the variable in question was assigned values inside two "if" conditions. These conditions were complementary, but the warning came up because there could be a case when none of the conditions were true and the variable would thus remain uninitialised.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM