Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:需要习惯循环吗?只是有点困惑?_Python_Loops_For Loop_While Loop - Fatal编程技术网

Python:需要习惯循环吗?只是有点困惑?

Python:需要习惯循环吗?只是有点困惑?,python,loops,for-loop,while-loop,Python,Loops,For Loop,While Loop,我该如何使用while循环添加从1到100的数字,然后使用for循环再次使用while执行程序 count=0 b=0 while count <100: count+=1 b+=count 下面是使用while循环执行此操作的多种方法之一的示例 iteration = 0 sum = 0 while iterations<len(range(1,101)): #You may need to add or subtract one from the left si

我该如何使用while循环添加从1到100的数字,然后使用for循环再次使用while执行程序

count=0
b=0
while count <100:
    count+=1
    b+=count

下面是使用while循环执行此操作的多种方法之一的示例

iteration = 0
sum = 0
while iterations<len(range(1,101)): #You may need to add or subtract one from the left side of the inequality 
    sum+=range(1,101)[iteration] 
    iteration+=1
my_list=[0]
计数=0
当计数小于100时:
计数=计数+1
我的列表。追加(计数)
打印(金额(我的清单))
计数=0
总数=0
对于范围(0101)内的计数:
总计=总计+计数
打印(总计)
这将起作用:

sum(xrange(101))

使用
reduce

>>> from itertools import *
>>> reduce(lambda x,y:x+y,range(1,101))
    5050
使用
求和

>>> sum(range(1,101))
    5050

“有点困惑”有点不明确-哪一部分比较困惑?你读过关于和循环的基本教程吗?如果读过,还有什么不清楚的地方吗?@imapython这里还有一个类似的问题:它会回答你的问题。这完全是错误的。您正在制作列表,将打印出1、2、3、4。。。当问题要求求和时。还有一种很好的方法,只需一行:
lst=range(1101)
问题中的和在哪里?“我如何添加数字1到100…”现在,在不查看我编写的代码的情况下相应地编辑您的答案。有很多方法可以将数字1添加到100。哦,再修一次。这就像一个斐波那契数。请包括更多的解释/上下文,因为这会使答案更有用。
sum(xrange(101))
>>> from itertools import *
>>> reduce(lambda x,y:x+y,range(1,101))
    5050
>>> sum(range(1,101))
    5050