变量已定义但未定义

时间:2019-09-12 23:17:38

标签: python python-3.x

我正在完成作业,但事实是,即使在整个作业中都定义了“重新启动”,该指令仍未定义。

我在本地位置上有错误,因此我将其设置为全局,但是现在它说“重新启动”是不确定的

def main():   

        global restart

        def changeMyList(myList):
            for i in range(len(myList)):
                myList[i] = myList[i].title()
            myList.sort()

        soccer_teams = ["Arsenal", "Chelsea", "Liverpool", "Barcelona", "Juventus", "Manchester City", "Atletico Madrid", "Borussia Dortmund"]

        team_length = len(soccer_teams)

        changeMyList(soccer_teams)

        print("|||||" + str(team_length) + " TEAMS" + "|||||")

        import random

        for team in soccer_teams:
            print(team)

        shuffle_user = input("Reshuffle? Y/N: ")
        if shuffle_user == 'y':
            random.shuffle(soccer_teams)
            print("List after first shuffle: ", soccer_teams)

            random.shuffle(soccer_teams)
            print("List after second shuffle: ", soccer_teams)
            restart = input("Run Again? Y/N: ").lower()

        if restart == 'y':
                        main()

        elif restart == 'n':
                        exit



main()

当我要求重新洗牌时按'n'时,它说“未定义名称'restart'”第32行; 40行

3 个答案:

答案 0 :(得分:1)

此代码中唯一定义restart的位置是:

restart = input("Run Again? Y/N: ").lower()
if shuffle_user == 'y':块中

。因此,如果对改组提示回答“否”,则不会为restart变量赋值,并且尝试读取该变量会产生NameError

答案 1 :(得分:0)

if(cmd.conf.friendOnly&& message.guild.id !== client.friendsList.friends.id){ return message.channel.send(notFriendError); } 定义在restart内,这就是为什么您无法访问它的原因,作用域位于if内:

if

所以您必须将它向上移动到if之外:

restart = input("Run Again? Y/N: ").lower()

答案 2 :(得分:0)

您很少需要使用global关键字。在您的情况下,最简单的解决方案是将重新启动作为参数传递给main()。

更新

我拿走了完整的代码并对其进行了修改,因此没有任何混乱。另外,我将其命名为restart some_globally_accessible_var,以使情况更加清楚。在您的确切情况下,实际上不必全局定义restart

如果您仍然对变量范围感到困惑,this似乎是对它的一个很好的解释。另外,here是一个很好的StackOverflow问题,涉及if语句中的变量范围。

some_globally_accessible_var = ''

def main(restart):   
        def changeMyList(myList):
            for i in range(len(myList)):
                myList[i] = myList[i].title()
            myList.sort()

        soccer_teams = ["Arsenal", "Chelsea", "Liverpool", "Barcelona", "Juventus", "Manchester City", "Atletico Madrid", "Borussia Dortmund"]

        team_length = len(soccer_teams)

        changeMyList(soccer_teams)

        print("|||||" + str(team_length) + " TEAMS" + "|||||")

        import random

        for team in soccer_teams:
            print(team)

        shuffle_user = input("Reshuffle? Y/N: ")
        if shuffle_user == 'y':
            random.shuffle(soccer_teams)
            print("List after first shuffle: ", soccer_teams)

            random.shuffle(soccer_teams)
            print("List after second shuffle: ", soccer_teams)
            restart = input("Run Again? Y/N: ").lower()

        if restart == 'y':
                        main(restart) #remember to pass restart to your function here!

        elif restart == 'n':
                        exit



main(some_globally_accessible_var)

有关错误的说明,请参见pppery的答案。