我想从用户那里获取字符串列表,我应该如何获取?
I want to get a list of strings from a user, how should i get that?
我想从用户那里获取一系列字符串,并将其放入列表中,然后打印出来
我还想在完成后关闭列表并打印它
list = []
for i in list:
list[a]=input('the name of stings:')
list.append(list[a])
a +=
print(list)
试试这个:
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']
你可以这样做:
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']
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)
示例如下:
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']
我想从用户那里获取一系列字符串,并将其放入列表中,然后打印出来
我还想在完成后关闭列表并打印它
list = []
for i in list:
list[a]=input('the name of stings:')
list.append(list[a])
a +=
print(list)
试试这个:
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']
你可以这样做:
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']
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)
示例如下:
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']