0

我是学习 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 

不知道怎么解决 非常感谢

4

1 回答 1

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. 之后添加 a91["..."是不合法的,整数是不可下标的。
  2. 应用适当的缩进。缩进在python中很重要,你需要小心!缩进不当的代码可能/将会表现得与您所期望的非常不同。

产生:

>>> 
holy grail
1975
terry jones
91
gramham chamman
['michael palin', 'john crees', 'eric idle', 'terry jones']
于 2013-05-02T07:23:21.457 回答