Python UnboundLocalError: local variable ‘str‘ referenced before assignment
0x00 错误
Traceback (most recent call last):File "/Users/apple/Desktop/Me/My/Python3/MyCode/test.py", line 43, in <module>test()File "/Users/apple/Desktop/Me/My/Python3/MyCode/test.py", line 13, in tests = str(count)
UnboundLocalError: local variable 'str' referenced before assignment
0x01 代码
代码:
import requests
from bs4 import BeautifulSoup as bsdef test():count = 0while True:count += 1s = str(count)print('第 ' + s + ' 次')breakprint('begin...')headers = {'user-agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Mobile Safari/537.36'}url = 'https://www.xxx.xxxx'r = requests.get(url, headers = headers)soup = bs(r.text, 'lxml')kk = soup.find(class_='xxxxx')str = kk.textarr = str.split('个')if len(arr) == 2:count = arr[0]if len(count) > 0:if int(count) > 10:print(str)if __name__ == '__main__':test()
数字转字符串不就是这样么?
count = 0
str(count)
全局搜索了一下代码
原来在后面定义了一个 str
变量
这个变量,覆盖了系统的类 str
所以,导致了
UnboundLocalError: local variable 'str' referenced before assignment
0x02 办法
既然是命名覆盖,那就换个名字好了~
要注意的一件事情是:
不要跟系统的类重名了!
Python UnboundLocalError: local variable ‘str‘ referenced before assignment相关推荐
- python UnboundLocalError: local variable 'log_f' referenced before assignment 错误
在写一个python程序,用finally处理异常的时候,报了"UnboundLocalError: local variable 'log_f' referenced before ass ...
- python 错误--UnboundLocalError: local variable 'num' referenced before assignment
val = 9 def test(flag): if flag: val = 1 else: print("test") return val if __name__ == '__ ...
- UnboundLocalError: local variable ‘a‘ referenced before assignment(Python报错解决)
问题提出 使用Python编程的时候,要注意函数内可以访问全局变量,但不能更新(修改)其值. 比如: a = 10 def sum ( n ) :n += aprint ('a = ', a, end ...
- 成功解决local variable 'str' referenced before assignment
成功解决local variable 'str' referenced before assignment 目录 解决问题 解决思路 解决方法 解决问题 local variable 'str' re ...
- 【debug】UnboundLocalError local variable a referenced before assignment
1)下面这种情况是不会报错的: >>> x = 10 >>> def bar(): ... print(x) >>> bar() 10 (2)但是 ...
- UnboundLocalError: local variable ‘loss’ referenced before assignment解决方法
UnboundLocalError: local variable 'loss' referenced before assignment 文章目录 UnboundLocalError: local ...
- UnboundLocalError: local variable ‘XXX‘ referenced before assignment解决办法
一.举例: 计算a到10的和 sum=0 def func(a):while a<=10:sum+=aa+=1return sum print(func(9)) 运行结果: UnboundLoc ...
- UnboundLocalError: local variable 'end_page' referenced before assignment
UnboundLocalError: local variable 'end_page' referenced before assignment 错误信息是在指出一个在使用之前没有赋值的局部变量.在 ...
- Python 解决 local variable ‘xxx‘ referenced before assignment
解决 local variable 'xxx' referenced before assignment 在刷leetcode的时候,定义全局变量后在函数中调用总是会出现这个错误,一直没搞明白,现在记 ...
- Python 读写当前路径下文件错误 UnboundLocalError: local variable 'file' referenced before assignment
python读取文件是个比较常用的操作,最近我在读取文件的时候却遇到下面这个问题. 我的代码是: try:file = open("./logs/test")... finally ...
最新文章
- Redis 限流的 3 种方式,还有谁不会!
- 超详细解读ORB-SLAM3单目初始化(下篇)
- 浪潮集团执行总裁王柏华:这次人工智能产业发展大潮“是真的”
- oracle归档原理,增加ORACLE归档位置到NFS(转)
- u-boot-2011.06-rc2移植到mini2440开发板(一)
- asp源码-魔方微店商城系统 v1版本源码
- iphone分辨率_揭示真实的手机屏幕大小和分辨率
- iOS中TextView显示HTML文本
- linux 欢迎信息
- Linux下PHP5.5编译参数详解
- ArcGIS多面体(multipatch)解析——引
- python基于大数据的食物推荐系统
- java 正则表达式 提取ip_使用正则表达式从字符串中提取IP地址
- GD32F103VET6替代STM32F103VET6遇到的问题
- oracle19c报价_Oracle 19C EM
- 简单爬取网易云音乐评论(新手报到)
- 最短路径(信息学奥赛一本通 - T1378)
- 小程序开发语言python_微信小程序是用什么语言开发的呢
- macOS Xcode8安装RVM,安装Ruby,安装/卸载Cococapods全程详解
- 如何录制真人出镜?别急,一篇教会你:真人出镜的ppt怎么录制