Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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/8/python-3.x/19.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
Python3输入循环_Python_Python 3.x - Fatal编程技术网

Python3输入循环

Python3输入循环,python,python-3.x,Python,Python 3.x,我使用for循环输入n单词 n = int(input("")) for i in range(n): a = input("") print(a) 当我输入: 3 1 1 1 2 它允许我输入n+1单词 n = int(input("")) for i in range(n): a = input("") print(a) 无法输出n+1字 我只想输出n个单词,然后与C中的语法相等: int a = 0; for(int i=0; i<n; i++)

我使用
for
循环输入
n
单词

n = int(input(""))
for i in range(n):
    a = input("")
    print(a)
当我输入:

3
1
1
1
2
它允许我输入
n+1
单词

n = int(input(""))
for i in range(n):
    a = input("")
    print(a)
无法输出
n+1

我只想输出n个单词,然后与
C
中的语法相等:

int a = 0;
for(int i=0; i<n; i++)
    scanf("%d",&a);
inta=0;

对于(inti=0;i它在我尝试时正好运行了3次。 如果您想更明确地说明您正在做什么,您可以将范围(0,n)中的i设置为
,但这实际上不会改变任何事情

范围(n)中i的循环
将从0运行到n-1。 因此,如果输入3次,它将生成3次运行,
i
的值为0,1,2

n = int( input( "Enter the number of runs: " ) )
for item in range( 0, n ):
    a = input( "\tPlease Input value for index %d: "%item )
    print( a )
它生成了以下输出:

Enter the number of runs: 3
    Please Input value for index 0: 1
1
    Please Input value for index 1: 1
1
    Please Input value for index 2: 1
1

我不明白为什么这对您不起作用。请尝试此修改版本,以便更清楚地了解正在发生的事情:

n = int(input("Enter number of values: "))
for i in range(n):
    a = input("Enter value {} ".format(i+1))
    print("Value {0} was {1}".format(i+1, a))
从中得到的输出是:

输入值的数量:3 输入值1 值1为1 输入值2 1 值2为1 输入值3 2
值3为2

我认为您与循环打印的输出混淆了

如果在第一个
n=int(输入(“”)中输入
3
循环将从
0
转到
2
(包括)

在每一个循环中,你要求一个新的
a
值并打印它。因此,在第一个循环之后,你输入
1
,它输出
1
(因为它打印它)。在第二个循环中,你输入另一个
1
,它打印它。最后你输入
2
,它也打印它

第一个循环:

input: 1
output: 1
第二个循环:

input: 1
output: 1
第三圈:

input: 2
output: 2
这就是为什么如果我运行以下命令

>>> n = int(input(""))
3
>>> for i in range(n):
...     a = input("")
...     print a
...
1
1
2
2
3
3
我得到6个数字(输入和输出)。您可以通过以下示例更清楚地看到这一点:

>>> n = int(input("Input: "))
Input: 3
>>> for i in range(n):
...     a = input("Input: ")
...     print "Output: " + str(a)
...
Input: 1
Output: 1
Input: 2
Output: 2
Input: 3
Output: 3

该代码对我来说工作正常。您是如何运行它的?您是在终端中还是在IDE中运行它?将提示字符串传递到
input
,例如
input(“>”
),以验证它实际上是接受n+1个字的
input
。您的C代码不会向输出写入任何内容。