我是Python新手。我正在尝试编写一个“for”循环,在每次迭代期间接受来自用户的数字。循环结束后,会输出数字的和。循环应运行 5 次。 我不知道如何对输入的数字求和。这是我到目前为止的代码,其中 z 应该是五个输入数字的总和:
for i in range(5):
y = int(input("Enter a whole number here:"))
print("The sum of these numbers is {}.".format(z))
在循环之前初始化总计变量。在循环内,将每个数字添加到总数中。
z = 0
for i in range(5):
y = int(input("Enter a whole number here:"))
z += y
print("The sum of these numbers is {}.".format(z))