Python While 循环语句使用else语句

2023-06-12,,

语法:

while 判断条件:

代码块1

else:

代码块2

说明:

当判断条件为true时,执行代码块1,否则执行else后的代码块2

示例:

#!/usr/bin/python

# -*- coding: UTF-8 -*-

# 定义变量,通过赋值运算符赋值“=”

count = 0;

# 当count值小于9时输出当前count的值,count大于9后进入else部分

while count < 9:

count += 1;

print 'current is: ',count;

else:

print 'exit while';

print 'the end';

代码截图:

运行截图:

查看更多技术请移步:https://ui.29mn.com

《Python While 循环语句使用else语句.doc》

下载本文的Word格式文档,以方便收藏与打印。