python 3.3.1尝试解决if else条件

时间:2013-05-02 07:17:33

标签: python-3.3

我是学习python 3的新手,并且请求帮助 代码:

movies = ["holy grail",1975,"terry jones",91
  [ "gramham chamman",
    ["michael palin","john crees","eric idle","terry jones"]]]
        for each_item in movies:
if isinstance (each_item,list):
    for nested_item in each_item :
        print (nested_item)
                    else:
                    print (each_item)



# when i type the next line with  " else : " 
  the program (python shell) told me syntax error 

我不知道如何解决它 非常感谢你

1 个答案:

答案 0 :(得分:2)

我相信这就是你要找的东西:

movies = ["holy grail",1975,"terry jones",91,[ "gramham chamman",
    ["michael palin","john crees","eric idle","terry jones"]]]
for each_item in movies:
    if isinstance (each_item,list):
        for nested_item in each_item :
            print (nested_item)
    else:
        print (each_item)

变化

  1. 在91之后添加,91["..."不合法,但是不能订阅。
  2. 适当缩进。在python中缩进重要,你需要小心!不正确的缩进代码可能/将与您期望的行为完全不同。
  3. 产地:

    >>> 
    holy grail
    1975
    terry jones
    91
    gramham chamman
    ['michael palin', 'john crees', 'eric idle', 'terry jones']