名称“时代”在全局声明之前使用 - 但它已声明
我正在编写一个小程序,用于计时并以有序的方式显示我的魔方解决方案。但是 Python (3) 一直困扰着我在全局声明之前使用的时间。但奇怪的是,它在一开始就被声明为 times = []
(是的,它是一个列表),然后又在函数上(这就是他抱怨的地方)声明为 times = [some, odd, list]
并使用 global times
对其进行“全球化”。这是我的代码,因此您可以根据需要对其进行分析:
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 世界的新手。
I'm coding a small program to time and show, in a ordered fashion, my Rubik's cube solvings. But Python (3) keeps bothering me about times being used prior to global declaration. But what's strange is that IT IS declared, right on the beggining, as times = []
(yes, it's a list) and then again, on the function (that's where he complains) as times = [some, weird, list]
and "globaling" it with global times
. Here is my code, so you may analyse it as you want:
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()
Any help would be very appreciated as I'm new in the world of Python.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
全局声明是当你声明
times
是global
时,如果一个变量被声明为
global
,那么在声明之前就不能使用它。在这种情况下,我认为您根本不需要声明,因为您没有分配给
times
,只是修改它。The global declaration is when you declare that
times
isglobal
If a variable is declared
global
, it can't be used before the declaration.In this case, I don't think you need the declaration at all, because you're not assigning to
times
, just modifying it.来自 Python 文档:
https://docs.python.org/reference/simple_stmts.html#global
因此,将
global times
移动到函数顶部应该可以解决这个问题。但是,在这种情况下您应该尽量不要使用
global
。考虑使用一个类。From the Python documentation:
https://docs.python.org/reference/simple_stmts.html#global
So moving
global times
to the top of the function should fix it.But, you should try not to use
global
in this situation. Consider using a class.来自 Python 文档
From the Python Docs
那应该有效。 “global[varname]”必须从定义开始;)
that should work. The "global[varname]" have to be at start from definition ;)
该程序应该可以运行,但可能无法完全按照您的预期运行。请注意这些变化。
This program should work but may not work exactly as you intended. Please take note of the changes.
我得到了同样的错误如下:
当尝试在inner()中使用本地和全局变量
x
时,如下所示:并且,当尝试使用inner()中的非局部变量和全局变量
x
如下所示:所以,我将
x
重命名为y
对于局部变量,如下所示:然后,错误得到解决,如下所示:
并且,我将非局部变量的
x
重命名为y
,如下所示:然后,错误就解决了,如下图:
I got the same error below:
When trying to use the local and global variables
x
in inner() as shown below:And, when trying to use the non-local and global variables
x
in inner() as shown below:So, I renamed
x
toy
for the local variable as shown below:Then, the error was solved as shown below:
And, I renamed
x
toy
for the non-local variable as shown below:Then, the error was solved as shown below:
对于主程序,可以在顶部声明。不会有任何警告。但是,如前所述,全局提及在这里没有用。主程序中放置的每个变量都位于全局空间中。在函数中,您必须使用 this 关键字声明您想要使用全局空间。
For the main program, you can declare it on the top. Ther will be no warning. But, as said, the global mention is not useful here. Each variable put in the main program is in the global space. In functions, you must declare that you want use the global space for it with this keyword.
我只是记录一些读书笔记,如有错误,欢迎指正。
感谢@Super Kai - Kazuya Ito 提供的示例。这让我想起了在Python中
因此,如果要定义一个全局变量,则无需在函数外部使用
global x
进行声明,只需执行x=1 # Global variable
即可。需要在函数内部使用
global x
才能调用函数外部定义的全局变量,或者更改全局变量。否则,变量x
在函数内部仍然未定义,除非您按如下方式命名另一个内部变量“x=4”:这给出了输出
I am just recording some reading notes and it is more than welcome if any of the following is wrong.
Thanks for the examples by @Super Kai - Kazuya Ito. It reminds me that in python
Thus if one wants to define a global variable, there is no need to use
global x
to declare outside of function, just dox=1 # Global variable
.One needs to use
global x
inside of a function in order to call the global variable defined outside of the function, or to change the global variable. Otherwise the variablex
is still undefined inside the function, unless you name another inside variable `x=4' as follows:which gives the output