Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.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 如何在for循环中使用一个input语句创建不同的输出?_Python - Fatal编程技术网

Python 如何在for循环中使用一个input语句创建不同的输出?

Python 如何在for循环中使用一个input语句创建不同的输出?,python,Python,我想知道如何在for循环中用一个input语句创建不同的输出(参见下面的代码)。例如,我应该如何使每个输入语句读取输入数字1:,输入数字2:等等。我应该只有一个输入语句还是多个输入语句 times = int(input("Enter how many numbers you want to sum")) sum = 0 for i in range(0, times): numInput = int(input("Enter number")) sum = sum + nu

我想知道如何在for循环中用一个input语句创建不同的输出(参见下面的代码)。例如,我应该如何使每个输入语句读取
输入数字1:
输入数字2:
等等。我应该只有一个输入语句还是多个输入语句

times = int(input("Enter how many numbers you want to sum"))

sum = 0

for i in range(0, times):
    numInput = int(input("Enter number"))
    sum = sum + numInput
print("The total is", sum)

一次输入就足够了,因为您可以在输入提示中更改字符串,如下所示:

times = int(input("Enter how many numbers you want to sum? "))
sum = 0

for i in range(1, times+1):
    numInput = int(input("Enter number {}:".format(i)))
    # or use below code:
    # numInput = int(input("Enter number %d:" % (i)))
    sum = sum + numInput

print("The total is", sum)
要了解有关设置python字符串格式的更多信息,请参阅此链接:

int(输入(“输入数字{}”.format(i+1)))
?这似乎很有效,谢谢。你用的那个工具叫什么?工具?这就是python
str.format
我现在正在学习python,到目前为止我还没有学会如何在输入语句中使用format。在打印语句中,是的,但不是在输入语句中。这就是为什么我要问。不要将信息放在场外的图像中。如果相关,则手动将其添加到问题中,否则,将其删除。特别是如果链接后面的信息消失了,那么您的问题对于以后的用户来说将变得不完整。使用
%
而不是
str.format
或f-strings来编写此代码没有什么错,但是对于教rank新手,我通常会选择另一种方式,仅仅是因为教授
%
意味着他们往往会在学习理解之前遇到单元组参数怪癖。感谢您的反馈,我更改了答案并添加了一个链接以获取更多信息