fn(n)
在Python2.7下面运行结果:
请输入正整数的位数:5
54748
92727
93084
Process finished with exit code 0
但在Python3下面运行结果:
请输入正整数的位数:5
Traceback (most recent call last):
File "D:/Program Files/JetBrains/PyCharm 2017.1.5/myPY/myPYPro/lesson001.py", line 18, in <module>
fn(n)
File "D:/Program Files/JetBrains/PyCharm 2017.1.5/myPY/myPYPro/lesson001.py", line 11, in fn
for k in range(0,len(rs)):
TypeError: object of type 'map' has no len()
Process finished with exit code 1
因为提示是:TypeError: object of type 'map' has no len()
所以直接把代码简化,输出list看看
简化代码如下:
rs = []
for i in range(100,1000):
rs = map(int, str(i))
print(rs)
在Python2.7下面运行结果:
[9, 9, 9]
Process finished with exit code 0
但在Python3下面运行结果:
<map object at 0x00C6E530>
Process finished with exit code 0
好吧,这就明白了,Python3下发生的一些新的变化,再查了一下文档,发现加入list就可以正常了
在Python3中,rs = map(int, str(i)) 要改成:rs = list(map(int, str(i)))
则简化代码要改成如下:
rs = []
for i in range(100,1000):