繁体   English   中英

Python 3.x while循环

[英]Python 3.x while loop

所以这是练习的想法:

我必须编写以下代码:

count = int(input())
while count>0:
    print (count)
    count = count - 1

从计数中求和,即:

5 5
4 9
3 12
2 14
1 15

我已经完成了这一部分,但是下一部分我有问题。

3 3
2 7
1 8

我的代码如下:

count = int(input())
sum = count
while count>0:
    print("%d %d"%(count, sum))
    count = count - 1
    sum = sum + count 

我不知道如何从2变到7,再从1变到8。

预先谢谢您,对于这个愚蠢的主题名称,我感到非常抱歉,但是我真的不知道该如何表达问题。

您的代码无法按原样工作。

您需要读入每个数字,而不仅仅是读入一个数字,您需要对每个数字求和(而不是对值求和两次,并且不减少计数,该计数最初只是用来循环多次。

假设输入0表示停止,并且没有任何错误检查:

count = int(input())
sum = 0
while count>0:
  sum = sum + count
  print (count, sum)

  # next input
  count = int(input())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM