如何在python中调试和修复语法错误

时间:2019-05-20 19:04:14

标签: python python-2.7

我正在尝试将这两个列表合并为字符串“一个阿布很聪明”。

list1=["a","ab","i","smar"]
list2=["t","s","u","n",]
def merge_list(list1, list2):
    merged=""
    b=-1
    result=''.join([str(a) + b for a,b in zip(list1,list2)])
    return result

但是我遇到了这个错误:

Traceback (most recent call last):
  File "main.py", in <module>
      merged=merge_list(list1,list2)
  File "main.py",in merge_list
      result=''.join([str(a) + b for a,b in zip(list1,list2)])
  File "main.py",in <listcomp>
      result=''.join([str(a) + b for a,b in zip(list1,list2)])
  TypeError: Can't convert 'NoneType' object to str implicitly

我该如何解决?

1 个答案:

答案 0 :(得分:0)

list1=["a","ab","i","smar"]
list2=["t","s","u","n"]
def merge_list(list1, list2):
    merged=""
    b=-1
    result=' '.join([str(a) + b for a,b in zip(list1,list2[::-1])])
    return result

print(merge_list(list1, list2))

输出

'an abu is smart'