无法在python 3中导入字符串
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法在python 3中导入字符串相关的知识,希望对你有一定的参考价值。
import random
import string
def pw_gen(size = 8, chars = string.ascii_letters + string.digits + string.punctuation):
return ''.join(random.choice(chars) for _ in range(size))
print(pw_gen(int(input('How many characters in your password'))))
我正在尝试运行此代码,但是在导入string
时,我收到此错误:
There are 10 types of people.
Those who know binary and those who don't.
I said: %r.
Traceback (most recent call last):
File "passgen1.py", line 3, in <module>
import string
File "D:pythonstring.py", line 9, in <module>
print ("I said: %r.") % x
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'
答案
在您的string.py
模块中,似乎是此代码:print ("I said: %r.") % x
哪个像这样评估:
- [
print("I said: %r.")
被执行,结果为None
(因为- 此值(为
None
)插入到上面的表达式中,因此计算为None % x
。这会导致您看到错误消息。似乎您打算写:
print("I said: %r." % x)
即,首先在字符串中插入x
,然后打印它。
以上是关于无法在python 3中导入字符串的主要内容,如果未能解决你的问题,请参考以下文章
无法在 Python 3.5.2 中导入 itertools
无法在 Python 2.x 和 Python 3.x 中导入 turtle 模块
在 python 3.4 中导入 tensorflow 时出错“无法加载本机 TensorFlow 运行时”