Python 使用WHILE循环创建一个程序,计算从1到100的数字相加量(例如:1+;2+;3+;4+;5…等等)
我有一个家庭作业要创建一个程序,使用while循环计算从1到100(1+2+3+4+5…)的数字相加量 我尝试了下面提供的代码!但问题是,我已经知道了数量,但我需要让程序计算它 我试过的代码:Python 使用WHILE循环创建一个程序,计算从1到100的数字相加量(例如:1+;2+;3+;4+;5…等等),python,while-loop,Python,While Loop,我有一个家庭作业要创建一个程序,使用while循环计算从1到100(1+2+3+4+5…)的数字相加量 我尝试了下面提供的代码!但问题是,我已经知道了数量,但我需要让程序计算它 我试过的代码: amount = 0 while amount <= 5050: amount += 1 print("The amount is: " + str(amount)) 金额=0 而amount那么你现在要做的就是加1,直到达到5050。相反,您希望将数字1添加到100。因此,解决方
amount = 0
while amount <= 5050:
amount += 1
print("The amount is: " + str(amount))
金额=0
而amount那么你现在要做的就是加1,直到达到5050。相反,您希望将数字1添加到100。因此,解决方案是有两个变量——一个表示到目前为止的总和(可以是金额
),另一个表示要添加的数字。对于每次迭代,您将继续增加您添加的数量,直到您将100添加到您的运行总数
amount = 0
to_add = 1
while to_add <= 100:
amount += to_add
to_add += 1
i=0
总和=0
当我开始时,忘记你知道结果。专注于我计算的总数。看看这有没有什么意义。你能把它写下来作为一个条件吗?添加一些描述。
amount = 0
for i in range(1, 101):
amount += i
i=0
sum=0
while i<=100:
sum+=i
i+=1
print(sum)