'NoneType' 对象在 Python 3 中的列表修饰符函数中没有属性 'strip'"
'NoneType' object has no attribute 'strip'" on my list modifier function in Python 3
我是 Python 新手。我正在尝试定义一个函数,它接受一个字符串列表并向其元素添加 html 标签。
例如,它修改:
list_1 = ["hey", "hello", "Joe"]
到
html_list = ["<ul>","<li>hey</li>", "<li>hello</li>", "<li>Joe</li>",> "</ul>"]
它引发了以下异常:
'NoneType'对象没有属性'strip'
这是我的代码:
def html_list(input_list):
for index in range(len(input_list)):
input_list[index] = "<li>" + input_list[index] + "<li>"
input_list.insert(0,"<ul>")
input_list.append("<ul>")
print(input_list)
list_1 = ["hey", "hello", "Joe"]
html_list(list_1)
我没有收到你的错误,但它可能源于你创建的与函数 html_list
同名的变量,也许?
我在 python2 和 python3 中测试了 this 代码,它似乎可以工作。我修改了代码,让你的函数returns input_list
。它在调用函数的地方打印输出。
list_1 = ["hey", "hello", "joe"]
def html_list(input_list):
new_list = ["<ul>"]
for item in input_list:
new_list.append("<li>{}</li>".format(item))
new_list.append("</ul>")
return new_list
print(html_list(list_1))
Returns
['<ul>', '<li>hey</li>', '<li>hello</li>', '<li>joe</li>', '</ul>']
我是 Python 新手。我正在尝试定义一个函数,它接受一个字符串列表并向其元素添加 html 标签。 例如,它修改:
list_1 = ["hey", "hello", "Joe"]
到
html_list = ["<ul>","<li>hey</li>", "<li>hello</li>", "<li>Joe</li>",> "</ul>"]
它引发了以下异常: 'NoneType'对象没有属性'strip'
这是我的代码:
def html_list(input_list):
for index in range(len(input_list)):
input_list[index] = "<li>" + input_list[index] + "<li>"
input_list.insert(0,"<ul>")
input_list.append("<ul>")
print(input_list)
list_1 = ["hey", "hello", "Joe"]
html_list(list_1)
我没有收到你的错误,但它可能源于你创建的与函数 html_list
同名的变量,也许?
我在 python2 和 python3 中测试了 this 代码,它似乎可以工作。我修改了代码,让你的函数returns input_list
。它在调用函数的地方打印输出。
list_1 = ["hey", "hello", "joe"]
def html_list(input_list):
new_list = ["<ul>"]
for item in input_list:
new_list.append("<li>{}</li>".format(item))
new_list.append("</ul>")
return new_list
print(html_list(list_1))
Returns
['<ul>', '<li>hey</li>', '<li>hello</li>', '<li>joe</li>', '</ul>']