python循环判断语法 while... else,  for... else

while… else 是python的一种循环判断语法

就是在循环的同时做判断,很惊诧python居然有这种很爽的语法

count = 0 
while count < 5: 
	print count, " is less than 5" 
	count = count + 1 
else: 
	print count, " is not less than 5"

如果是java的话,就得这样来写了,还得判断结束时机

count = 0 
while (true){
	count = count + 1 
	if (count < 5) {
		print count, " is less than 5" 
	} else {
		print count, " is not less than 5"
		return;
	}
}

同理,for… else也是,这里不再赘述,如果有其他特殊情况没有考虑到,欢迎留言

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章