当我运行这段代码时,它会以UnboundLocalError响应:在赋值之前引用了局部变量'hhh'。但是,全局字符串“temp”不响应这样的错误,尽管以类似的方式定义。任何帮助都太好了,谢谢。你知道吗
import random, os
def start():
global level
global hhh
global temp
level=1
temp=' +-!'
hhh='[X'
os.system('CLS')
actualcrawl()
def actualcrawl():
print (temp)
for a in range(2,128):
hhh=hhh+temp[random.randrange(1,8)]
hhh=hhh[:79]+'>'+hhh[80:]
for i in range(1,3):
a=random.randrange(3,8)
b=random.randrange(6,15)
hhh=hhh[:16*a+b-1]+'='+hhh[16*a+b:]
for i in range(1,9):
print (hhh[16*i-16:16*i])
是的,你应该看看这个问题Using global variables in a function other than the one that created them
简而言之,如果它只是从一个名称中读取,而该名称在本地不存在,它将尝试在任何包含望远镜。那个这就是
temp
的情况,它将在全局范围。但是有了hhh
,您就可以编写了,这将使Python相信hhh
是一个局部变量。你知道吗还有一件事,但更重要的是,不建议使用
global
,您可以在start()
中调用actualcrawl()
,然后传入hhh,temp
,这是大多数人的做法。你知道吗编辑
很简单:
我不知道在Python之前使用什么语言,但是你不需要声明一个变量,比如C/C++。因为当你分配给一个变量时,你只是把名字绑定到一个对象。看到了吗这个python variables are pointers?
相关问题 更多 >
编程相关推荐