我已经编写了一段代码,它工作正常,只是我正在使用append() 时遇到一个问题,我知道append 在列表末尾添加了元素。 我希望列表不要从空白元素开始。
`#this function adds append items to list by taking input from the user
add_item_to_list= input("Enter item to add to list :")
my_list.append(add_item_to_list)
my_list=[" "]
another_item=input("add another item y/n ?")
while another_item=="y" :
add_item_to_list= input("Enter item to add to list :")
another_item=input("add another item y/n ?")
my_list.append(add_item_to_list)
print(my_list)`
您正在初始化
my_list=[" "]
添加一个blank
元素(正如您所说)。
不用它就初始化它:
my_list = []