0

我想从用户那里获取一系列字符串并将其放入列表中,然后打印出来

我也想当我完成时关闭列表并打印它

list = []
for i in list:

    list[a]=input('the name of stings:')
    list.append(list[a])
    a +=
    print(list)
4

4 回答 4

1

试试这个 :

list_ = []
not_done = True
while not_done:
    inp = input('name of string : ')
    if inp.lower() != 'done': # Put any string in stead of 'done' by which you intend to not take any more input
        list_.append(inp)
    else:
        break
print(list_)

输出

name of string : sd
name of string : se
name of string : gf
name of string : yh
name of string : done
['sd', 'se', 'gf', 'yh']
于 2019-05-23T09:22:19.680 回答
1

你可以这样做:

n = int(input())

my_list = list()
for i in range(n):
    my_str = input('Enter string ')
    my_list.append(my_str)
    print('You entered', my_str)
    print(my_list)

这是示例(第一行取数字,表示您想要输入多少次):

4
Enter string abc
You entered abc
['abc']
Enter string xyz
You entered xyz
['abc', 'xyz']
Enter string lmn
You entered lmn
['abc', 'xyz', 'lmn']
Enter string opq
You entered opq
['abc', 'xyz', 'lmn', 'opq']
于 2019-05-23T09:23:25.547 回答
0
N = 10  # desired number of inputs

lst = []  # don't use `list` as it's resereved
for i in range(N):
    lst.append(input('the name of strings: ')

print(lst)
于 2019-05-23T09:27:41.373 回答
0

一个例子如下所示:

input_list = []
while True:
    your_input = input('Your input : ')
    if your_input.upper() == "DONE":
        break
    input_list.append("%s" % your_input )
print("%s" % input_list)

输出:

>>> python3 test.py 
Your input : a
Your input : b
Your input : c
Your input : d
Your input : dOnE
['a', 'b', 'c', 'd']
于 2019-05-23T10:21:32.747 回答