運行時報錯:SyntaxError: 'break' outside loop。
原因:break只能在for和while循環中使用。
報錯的具體例子
>>> def func(L): ... result = {} ... if not isinstance(L,list): ... print("類型不正確") ... break ... File "<stdin>", line 5 SyntaxError: 'break' outside loop
解決方法:
>>> def func(L): ... result = {} ... while not isinstance(L,list): ... print("類型不正確") ... break ... >>> func([1,2,3,4]) >>> func("qwe") 類型不正確