我在PC上运行Python 2.7.8。
但是我正在阅读的本书使用的是Python 3.0,下面的代码将打印项目和嵌套项目(如果有)。
def print_it(the_list, level=0):
for each_item in the_list:
if isinstance(each_item, list):
print_it(each_item, level+1)
else:
for tab_stop in range(level):
print("\t", end='')
print(each_item)
这个特定的代码
for tab_stop in range(level):
print("\t", end='')
在嵌套列表项上插入标签。
但是,该代码不适用于Python 2.7.8。
在Python 2.7.8中,这等效于什么?
谢谢!
将以下内容添加到python代码的顶部。
from __future__ import print_function
这将确保print
只能将其用作Python 3中的函数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句