写了这么久的Python,现在才知道Python居然有嵌套限制,不得不说还挺好玩。
我制作了一个自动生成一百个嵌套if的代码,运行后居然Error了:
x=input("请输入要生成的文件:")
a=0
with open(x,'w')as f:
while a<100:
f.write(" "*a+"if %d==%d:\n"%(a,a))
a+=1
f.write(" "*a+"print('run ok')")
with open(x,'r') as f:
try:
exec(f.read())
print("神奇,居然没报错。")
except Exception as e:
print("报错了:",e)
报错内容如下:
我又添加了一点代码来检测Python嵌套的最高限制是多少:
n=1
while True:
a=0
data=""
while a<n:
data+=" "*a+"if %d==%d:\n"%(a,a)
a+=1
data+=" "*a+"print('run ok')"
try:
exec(data)
except:
print(n)
break
n+=1
最后输出100个(0~99)"run ok"和一个数字100
也就是说,Python嵌套的最高限制是100个嵌套!