|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我写的代码是:
- def a():
- text = input('请输入文本:')
- if '\n' in text:
- text_new = text.replace('\n','1')
- symbols = r'~!@#$%^&*()_+{}|:"<>?`-=[]\;,./'
- length = len(symbols)
- for i in range(length):
- b = text_new.count(symbols[i])
- print(symbols[i],'有',b,'个')
复制代码
运行以后出错了,错误是
- Traceback (most recent call last):
- File "<pyshell#20>", line 1, in <module>
- a()
- File "C:/Users/ciic/Desktop/111.py", line 8, in a
- b = text_new.count(symbols[i])
- UnboundLocalError: local variable 'text_new' referenced before assignment
复制代码
这里没看明白错误提示,两个疑问:1.我只有一个函数,为什么会提示local variable?2.我已经定义过text_new了,为什么说没有定义?
- def a():
- text = input('请输入文本:')
- text_new = text.replace('\n','1')
- symbols = r'~!@#$%^&*()_+{}|:"<>?`-=[]\;,./'
- length = len(symbols)
- for i in range(length):
- b = text_new.count(symbols[i])
- print(symbols[i],'有',b,'个')
- a()
复制代码
因为input默认返回的是换行符之前的字符,if判断之后才定义text_new,不用判断换行符的存在
|
|