python - UnboundLocalError:分配前引用的局部变量“uwu”
问题描述
哟,我试着做点什么,让我们说这是我的代码:
import threading
uwu = 0
def main():
def e():
if uwu > 10:
print("no u")
uwu += 1
elif uwu <= 10:
print('Hello, World!')
uwu += 1
done = False
while not done:
try:
if threading.active_count() < 100:
threading.Thread(target=e).start()
except IndexError:
done = True
print("Done!")
input()
main()
所以,当我执行它时,我得到了这个错误:
File "/usr/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "ask stacc xd.py", line 5, in e
if uwu > 10:
UnboundLocalError: local variable 'uwu' referenced before assignment
如果有人可以帮助我解决这个问题,请回答或评论。
解决方案
您指的是全局变量。尝试使用global
关键字。
import threading
uwu = 0
def main():
def e():
global uwu
if uwu > 10:
print("no u")
uwu += 1
elif uwu <= 10:
print('Hello, World!')
uwu += 1
done = False
while not done:
try:
if threading.active_count() < 100:
threading.Thread(target=e).start()
except IndexError:
done = True
print("Done!")
input()
main()
推荐阅读
- c++ - 未在此范围内声明的函数和变量(c++)
- python - “TypeError:'int' object is not callable”是什么意思?
- javascript - 在导入 ES6 中获取属性
- python - pygame.camera.Camera.start() 正在创建无关的缓冲区窗口
- unit-testing - 如何使用 Ceedling/Unity/Cmock 测试嵌入式系统
- php - MySQL 更新有 2 个条件
- angular - Angular 8 业力忽略服务请求
- c - 仅使用 fgets 检查整数而不检查字符串
- c# - 如何创建 powershell cmdlet (C#) 输出参数
- php - Nginx 负载平衡与代理 php-fpm - try_files