如果执行以下代码将显示错误消息:
UnboundLocalError:赋值前引用的局部变量'a'
a = 220.0
b = 4300.0
c = 230.0/4300.0
def fun():
while (c > a/b):
a = a + 1
print a/b
if __name__ == '__main__':
fun()
Run Code Online (Sandbox Code Playgroud)
但修改为:
a = 220.0
b = 4300.0
c = 230.0/4300.0
def fun():
aa = a
bb = b
while (c > aa/bb):
aa = aa + 1
print aa/bb
if __name__ == '__main__':
fun()
Run Code Online (Sandbox Code Playgroud)
它会好的.任何建议或指示都会很棒.非常感谢!
代码1:
# coding:utf-8
sum = 5
def add(x, y):
print sum
sum = x + y
if __name__ == '__main__':
add(7, 8)
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,出现以下错误:
ssspure:python ssspure$ python test.py
Traceback (most recent call last):
File "test.py", line 11, in <module>
add(7, 8)
File "test.py", line 6, in add
print sum
UnboundLocalError: local variable 'sum' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
代码2:
ssspure:python ssspure$ python test.py
Traceback (most recent call last):
File "test.py", line 11, in <module>
add(7, 8)
File "test.py", line 6, in add
print …Run Code Online (Sandbox Code Playgroud) 2我对 Python 比较陌生(使用 3.3.3)并且有一个与列表相关的问题。在函数内修改全局列表变量时(请不要讲全局变量的弊端),通常不需要在函数内使用 global 关键字声明列表 - 只要您坚持使用列表方法。特别是,您不能在不首先使用 global 关键字的情况下使用增强加法。让我感到惊讶的是,在函数外使用增广加法显然不会修改列表变量(仅列表内容),因此我希望可以在不使用 global 关键字的情况下在函数内使用它。这里有两个我无法调和的例子:
list_1 = []
def my_func():
list_1.append(0)
#list_1 += [0]
my_func()
print('list_1 =', list_1)
Run Code Online (Sandbox Code Playgroud)
list_1 = [0]正如预期的那样,这会打印,而注释掉的增强加法操作会生成一个关于在赋值之前使用局部变量的抱怨。
这是一个我无法与前一个协调的例子:
list_1 = [0]
list_2 = list_1
list_1 += [1]
print('list_2 =', list_2)
Run Code Online (Sandbox Code Playgroud)
这会打印list_2 = [0, 1],这向我表明list_1 += [1]没有修改 list_1 变量。我知道这list_1 = list[1] + [1]符合修改 list_1 的条件,但增强添加似乎没有。为什么在函数内部的增广加法需要使用 global 关键字?感谢您帮助理解这一点。
我认为list.extend列表中的"+ ="基本上做同样的事情 - 扩展列表而不创建新列表.
我希望下面的代码可以打印,[42, 43, 44, 45, 46]但我得到了UnboundLocalError: local variable 'x' referenced before assignment
为什么我收到此错误?区别在哪里?
def f():
x.extend([43, 44])
def g():
x += ([45, 46])
x = [42]
f()
g()
print x
Run Code Online (Sandbox Code Playgroud)
我在python2.7.3和python3.4.0中尝试过这个.
我迷失了,因为我做错了什么......我现在在网上搜了几个小时,试图重新格式化我的代码,现在我感觉卡住了.
这是我的代码:
import httplib
import json
urlBase = 'amoeba.im'
token = False
username = raw_input('Username? ')
connection = httplib.HTTPConnection(urlBase)
def get(url):
connection.request("GET", url)
response = connection.getresponse()
print response.status, response.reason
print response.read();
if token == False:
token = response.read()
token = token.split('"token":"')[1]
token = token.split('","')[0]
print token
get('/api/login?username=' + username)
get('/api/rooms/join?room=#lobby&token=' + token)
get('/api/postmessage?message=hello%20world&token=' + token)
connection.close()
Run Code Online (Sandbox Code Playgroud)
这是终端输出:
Tyler-Keohanes-MacBook-Pro:~ tylerkeohane$ clear && '/usr/bin/pythonw' '/Users/tylerkeohane/Desktop/chatbot.py'
Username? TgwizBot
200 OK
{"success":true,"username":"TgwizBot","token":"103f6a2809eafb6","users":[{"username":"razerwolf","seen":1338582178260},{"username":"tonynoname","seen":1338582178028},{"username":"arrum","seen":1338582177804},{"username":"Valerio","seen":1338582177504},{"username":"Tgwizman","seen":1338582177258},{"username":"tonynoname2","seen":1338582178004},{"username":"TgwizBot","seen":1338582182219}],"time":1338582182219}
Traceback (most recent call last):
File "/Users/tylerkeohane/Desktop/chatbot.py", line 21, in <module>
get('/api/login?username=' …Run Code Online (Sandbox Code Playgroud) 我运行此脚本时出现错误(标题中显示):
import psycopg2
conn = None
conn_string = "host='localhost' dbname='localdb' user='someuser' password='abracadabra'"
def connectDb():
if conn is not None: # Error occurs on this line
return
# print the connection string we will use to connect
print "Connecting to database\n ->%s" % (conn_string)
Run Code Online (Sandbox Code Playgroud)
conn具有全局范围,并且在函数中引用之前被赋值为None - 为什么出现错误消息?
file1.py:
a = 2;
class adder():
def __init__(self):
a = a;
a = a % 5;
print a;
Run Code Online (Sandbox Code Playgroud)
adder()在" UnboundLocalError: local variable 'a' referenced before assignment,"中实例化结果,但如果我将init更改为:
def __init__(self):
print a;
Run Code Online (Sandbox Code Playgroud)
然后我没有错误.