【问题标题】:Python While/For loopPython While/For 循环
【发布时间】:2016-06-07 18:04:55
【问题描述】:

我怎样才能把它变成一个while循环并输出同样的东西????

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)

【问题讨论】:

  • 这项任务显然是为了大揭秘sum(range(56,120)) 也请记住range(x,y) 真的只是去y-1

标签: python loops for-loop while-loop


【解决方案1】:
summation = 0
start_val = 56
stop_val  = 120
while start_val < stop_val:
    summation += start_val
    start_val += 1
print summation

【讨论】:

【解决方案2】:

有多种方法,但其中一种是:

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

【讨论】:

    【解决方案3】:
    sum = 0
    x = 56
    while x < 120:
        sum += x
        x += 1
    print(sum)
    

    和平!

    【讨论】:

    • 你能帮我写一段代码,打印 15 到 35 之间的 5 个随机偶数吗?
    • 很简单:使用 random.randint() 和 while 循环
    猜你喜欢
    • 2012-09-19
    • 1970-01-01
    • 2022-06-28
    • 2018-07-24
    • 1970-01-01
    • 2020-10-09
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多