Python While/For loop

浪尽此生 提交于 2019-12-12 06:59:29

问题


how can I make this into a while loop and output the same thing????

for x in range(56,120) :
    if (x < 57) :
          summation = 0
    summation = x + summation
    if (x == 119) :
          print (“Sum of integers from 56 to 120 is”, summation)

回答1:


summation = 0
start_val = 56
stop_val  = 120
while start_val < stop_val:
    summation += start_val
    start_val += 1
print summation



回答2:


There are various methods, but one of them is:

x = 56
while x < 120:
    ...
    x += 1



回答3:


sum = 0
x = 56
while x < 120:
    sum += x
    x += 1
print(sum)

peace!



来源:https://stackoverflow.com/questions/35619272/python-while-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!