退出循环,什么时候用break好,什么时候加条件让计算机自己判断
比如说这个题,直接while n<21好像还方便些,实际编程的时候,什么时候用break会比较方便呢
sum = 0
x = 1
n = 1
while n < 21:
sum = sum +x
n = n+1
x = x*2
print sum
比如说这个题,直接while n<21好像还方便些,实际编程的时候,什么时候用break会比较方便呢
sum = 0
x = 1
n = 1
while n < 21:
sum = sum +x
n = n+1
x = x*2
print sum
2018-01-30
#imooc网的爬虫的调度程序
def craw(self, root_url):
count = 1
self.urls.add_new_url(root_url)
while self.urls.has_new_url():
try:
new_url = self.urls.get_new_url()
print 'craw %d : %s' % (count, new_url)
html_cont = self.downloader.download(new_url)
#爬虫解析器,传入2个参数(当前url,下载完成的页面数据)
new_urls, new_data = self.parser.parse(new_url, html_cont)
self.urls.add_new_urls(new_urls)
self.outputer.collect_data(new_data)
if count == 1000:
break
count = count + 1
except:
print 'craw failed'
self.outputer.output_html()
举报