通过用户输入将项目/元素输入到空白列表

问题描述 投票:0回答:1

我已经编写了一段代码,它工作正常,只是我正在使用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)`
python list append
1个回答
0
投票

您正在初始化

my_list=[" "]
添加一个
blank
元素(正如您所说)。 不用它就初始化它:

my_list = []

© www.soinside.com 2019 - 2024. All rights reserved.