Python 2:在FOR循环中添加整数

Python 2:在FOR循环中添加整数,python,Python,我在课堂上做实验,遇到了这个问题: 编写一个程序,使用for语句作为计数循环,将用户输入的整数相加。首先,程序询问要加多少个数字。然后程序会提示用户输入每个数字。最后,它将打印总和。 我在回答这个问题时遇到了很多困难,如果有人能帮助我,那就太好了,谢谢 到目前为止,我写了以下内容: NumOfInt=int(raw_input("How many numbers would you like to add up?")) for i in range(NumOfInt): 我想

我在课堂上做实验,遇到了这个问题:

编写一个程序,使用for语句作为计数循环,将用户输入的整数相加。首先,程序询问要加多少个数字。然后程序会提示用户输入每个数字。最后,它将打印总和。

我在回答这个问题时遇到了很多困难,如果有人能帮助我,那就太好了,谢谢

到目前为止,我写了以下内容:

    NumOfInt=int(raw_input("How many numbers would you like to add up?")) 
    for i in range(NumOfInt):

我想这就是你要问的:

使用for循环编写一个程序,该程序将询问用户要加多少个数字。然后它会问他们这个数量的次数,这个数字将被加到总数中。然后将打印此文件

如果是这种情况,那么我相信您只需要向用户询问这个数量的数字,并以与您的类似的方式编写for循环:

NumOfInt = int(input("How many numbers would you like to add up? : "))
total = 0

for i in range (NumOfInt):
    newnum = int(input("Enter a number! : "))
    total += newnum

print("Your total is: " + str(total))
这将把他们的输入加到总数中,直到他们输入的数字超过NumOfInt:

How many numbers would you like to add up? : 4
Enter a number! : 1
Enter a number! : 2
Enter a number! : 3
Enter a number! : 4
Your total is: 10

我希望这能有所帮助:)

你能告诉我们到目前为止你都做了些什么吗?问我们一个问题。你对问题的哪一方面有困难?你有没有试过在你期望的时候不起作用的东西?你不能把你的作业发到我们这里。欢迎来到Stack Overflow!您似乎在要求某人为您编写一些代码。堆栈溢出是一个问答网站,而不是代码编写服务。请学习如何编写有效的问题。仅仅在没有解释的情况下转储代码不是一个有用的答案。很明显,OP了解如何获取输入并将其转换为整数。除了表明应该再次这样做之外,没有什么别的解释了。
How many numbers would you like to add up? : 4
Enter a number! : 1
Enter a number! : 2
Enter a number! : 3
Enter a number! : 4
Your total is: 10