【问题标题】:name 'times' is used prior to global declaration - But IT IS declared!名称 'times' 在全局声明之前使用 - 但它已声明!
【发布时间】:2011-01-07 12:23:44
【问题描述】:

我正在编写一个小程序来计时并以有序的方式显示我的魔方解法。但是 Python (3) 一直困扰着我在全局声明之前使用的时间。但奇怪的是,它在一开始就被声明为times = [](是的,这是一个列表),然后又在函数(他抱怨的地方)上声明为times = [some, weird, list],并用@987654323“全球化”它@。这是我的代码,你可以随意分析:

import time

times = []

def timeit():
    input("Press ENTER to start: ")
    start_time = time.time()
    input("Press ENTER to stop: ")
    end_time = time.time()
    the_time = round(end_time - start_time, 2)
    print(str(the_time))
    times.append(the_time)
    global times
    main()

def main():
    print ("Do you want to...")
    print ("1. Time your solving")
    print ("2. See your solvings")
    dothis = input(":: ")
    if dothis == "1":
        timeit()
    elif dothis == "2":
        sorte_times = times.sort()
        sorted_times = sorte_times.reverse()
        for curr_time in sorted_times:
            print("%d - %f" % ((sorted_times.index(curr_time)+1), curr_time))
    else:
        print ("WTF? Please enter a valid number...")
        main()

main()

任何帮助都将不胜感激,因为我是 Python 世界的新手 :)

【问题讨论】:

    标签: python python-3.x global-variables


    【解决方案1】:

    全局声明是当你声明timesglobal

    def timeit():
        global times # <- global declaration
        # ...
    

    如果变量声明为global,则不能在声明前使用。

    在这种情况下,我认为你根本不需要声明,因为你没有分配给times,只是修改它。

    【讨论】:

    • 该死!现在,当我选择选项 2 来显示我的结果时,会显示:AttributeError: 'NoneType' object has no attribute 'reverse',指的是 sorted_times = sorte_times.reverse()
    • 那是因为times.sort() 返回None。您应该使用times.sort(); print timesprint sorted(times)
    【解决方案2】:

    来自 Python 文档:

    全局语句中列出的名称不得在同一代码块中使用 在该全局语句之前的文本。

    https://docs.python.org/reference/simple_stmts.html#global

    所以将global times 移到函数顶部应该可以修复它。

    但是,在这种情况下,您应该尽量不要使用global。考虑使用一个类。

    【讨论】:

      【解决方案3】:

      来自Python Docs

      全局语句中列出的名称不得在该全局语句之前的同一代码块中使用。

      【讨论】:

        【解决方案4】:

        该程序应该可以运行,但可能无法完全按照您的预期运行。请注意更改。

        import time
        
        times = []
        
        def timeit():
            input("Press ENTER to start: ")
            start_time = time.time()
            input("Press ENTER to stop: ")
            end_time = time.time()
            the_time = round(end_time - start_time, 2)
            print(str(the_time))
            times.append(the_time)
        
        def main():
            while True:
                print ("Do you want to...")
                print ("1. Time your solving")
                print ("2. See your solvings")
                dothis = input(":: ")
                if dothis == "1":
                    timeit()
                elif dothis == "2":
                    sorted_times = sorted(times)
                    sorted_times.reverse()
                    for curr_time in sorted_times:
                        print("%d - %f" % ((sorted_times.index(curr_time)+1), curr_time))
                    break
                else:
                    print ("WTF? Please enter a valid number...")
        
        main()
        

        【讨论】:

          【解决方案5】:

          对于主程序,可以在顶部声明。不会有任何警告。但是,如前所述,全局提及在这里没有用。放在主程序中的每个变量都在全局空间中。在函数中,您必须使用 this 关键字声明要为其使用全局空间。

          【讨论】:

            【解决方案6】:
            import time
            
            times = []
            
            def timeit():
                global times
                input("Press ENTER to start: ")
                start_time = time.time()
                input("Press ENTER to stop: ")
                end_time = time.time()
                the_time = round(end_time - start_time, 2)
                print(str(the_time))
                times.append(the_time)
                main()
            
            def main():
                print ("Do you want to...")
                print ("1. Time your solving")
                print ("2. See your solvings")
                dothis = input(":: ")
                if dothis == "1":
                    timeit()
                elif dothis == "2":
                    sorte_times = times.sort()
                    sorted_times = sorte_times.reverse()
                    for curr_time in sorted_times:
                        print("%d - %f" % ((sorted_times.index(curr_time)+1), curr_time))
                else:
                    print ("WTF? Please enter a valid number...")
                    main()
            
            main()
            

            应该可以。 “global[varname]”必须从定义开始;)

            【讨论】:

            • 虽然此代码可以回答问题,但提供有关 如何 和/或 为什么 解决问题的附加上下文将改善答案的长期价值。
            • 我已经说过了。因为你必须在开始时写“global
            • 这是问题
            猜你喜欢
            • 2018-10-21
            • 2016-04-23
            • 2023-01-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-06-02
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多