为什么输出是“总和是55”? [英] Why the output is "the sum is 55"?

查看:136
本文介绍了为什么输出是“总和是55”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< pre>  用于说明条件在顶部的循环的程序 

尝试不同的数字
n = < span class =code-digit> 10

取消注释用户输入
n = int(输入(输入n:))

初始化和计数器
sum = < span class =code-digit> 0
i = 1

while i< = n:
sum = sum + i
i = i + 1 更新计数器

打印金额
print 总和是,总和)





我尝试过:



我不明白这一点,它没有点击我。

解决方案

引用:

为什么输出总和是55?



想看这段代码构建答案你自己?



一步一步在调试器上运行代码,并检查变量。



调试器在这里向您展示您的代码正在做什么。

调试器中没有魔法,它不知道您应该做什么,它没有发现错误。



要查看代码正在执行的操作:只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。 />
< a href =https://en.wikipedia.org/wiki/Debugger>调试器 - 维基百科,免费的百科全书 [ ^ ]

27.3。 pdb - Python调试器 - Python 3.6.1文档 [ ^ ]

使用Python进行调试Python征服宇宙 [ ^ ]

pdb - 交互式调试器 - 本周的Python模块 [ ^ ]

调试器只向您展示你的代码正在做,你的任务就是与它应该做的事情进行比较。


因为这就是你告诉它要做的事情!

看看每次绕圈时结束:

 i Sum 
2 1
3 3
4 6
5 10
6 15
7 21
8 28
9 36
10 45
11 55
金额为55


<pre># Program to illustrate a loop with condition at the top

# Try different numbers
n = 10

# Uncomment to get user input
#n = int(input("Enter n: "))

# initialize sum and counter
sum = 0 
i = 1 

while i <= n:
   sum = sum + i
   i = i+1    # update counter

# print the sum
print("The sum is",sum)



What I have tried:

I did not understand this,it is not clicking with me.

解决方案

Quote:

Why the output is "the sum is 55"?


Want to watch this code building the answer by yourself ?

Run your code on debugger step by step, and inspect variables.

The debugger is here to show you what your code is doing .
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs.

To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


Because that is what you told it to do!
Look at the values at the end each time you go round the loop:

 i Sum
 2   1
 3   3
 4   6
 5  10
 6  15
 7  21
 8  28
 9  36
10  45
11  55
The sum is 55


这篇关于为什么输出是“总和是55”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆