Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从循环python中的用户获取字符串列表作为输入_Python_Python 3.x - Fatal编程技术网

从循环python中的用户获取字符串列表作为输入

从循环python中的用户获取字符串列表作为输入,python,python-3.x,Python,Python 3.x,我是python新手,这是一个基本问题 如何从用户处获取字符串列表作为输入?我试过: array = [] for i in range(0,4): array[i] = input("Enter string") 这有一个错误。我知道我错了。如何获取字符串列表作为输入 您的数组大小为0,这就是您无法访问(或分配)任何元素的原因。正确的代码是: array = [] for i in range(4): array.append(input("Enter string"))

我是python新手,这是一个基本问题

如何从用户处获取字符串列表作为输入?我试过:

array = []
for i in range(0,4):
    array[i] = input("Enter string")

这有一个错误。我知道我错了。如何获取字符串列表作为输入

您的数组大小为0,这就是您无法访问(或分配)任何元素的原因。正确的代码是:

array = []
for i in range(4):
    array.append(input("Enter string"))

您的数组大小为0,这就是您无法访问(或分配)任何元素的原因。正确的代码是:

array = []
for i in range(4):
    array.append(input("Enter string"))
试试这个:

for i in range(4):
    array.append(input("Enter string >>"))
由于您的数组还没有任何值,分配给
数组[i]
将不起作用。

尝试以下操作:

for i in range(4):
    array.append(input("Enter string >>"))
由于您的数组还没有任何值,分配给
数组[i]
将不起作用。

简明:

array = [input("Enter string: ") for i in range(4)]
简明的:

array = [input("Enter string: ") for i in range(4)]

当范围限制未知时,即(n)需要作为用户输入时

n = int(input("Enter the number of names in list for input:"))
a = [input() for i in range(n)]

当范围限制未知时,即(n)需要作为用户输入时

n = int(input("Enter the number of names in list for input:"))
a = [input() for i in range(n)]

.append
它们。
array=[input(“enter string”)for uu在范围(4)]
将以一行的形式出现,我想要一个列表。可以像数组[i]一样访问。追加是否有助于此追加工作。我想这就像在c#中,它已经附加到一个字符串。谢谢
.append
它们。
array=[input(“enter string”)for uu.in range(4)]
将以一行形式出现,我想要一个列表。可以像数组[i]一样访问。追加是否有助于此追加工作。我想这就像在c#中,它已经附加到一个字符串。谢谢确实,这很优雅,但在python中,2x input()将字符串作为变量进行计算,并抛出一个NameError。在Python2X中,我建议使用raw_input()而不是input()。但由于这个问题是在python3x下标记的,所以最好是:感谢在使用python2的人尝试使用它时注意到这一点。链接到Python2文档中的相关条目:&这的确很优雅,但在Python2X input()中,它将字符串作为变量进行求值,并抛出NameError。在Python2X中,我建议使用raw_input()而不是input()。但由于这个问题是在python3x下标记的,所以最好是:感谢在使用python2的人尝试使用它时注意到这一点。Python2文档中相关条目的链接:&Works。谢谢@SekurazWorks。谢谢@Sekuraz