首页 > 编程语言 >python36

python36

时间:2022-12-16 13:01:03浏览次数:62  
标签:count less number python36 Enter print than

无限循环

如果条件判断语句永远为 true,循环将会无限的执行下去,如下实例:

实例

#!/usr/bin/python # -*- coding: UTF-8 -*- var = 1 while var == 1 : # 该条件永远为true,循环将无限执行下去 num = raw_input("Enter a number :") print "You entered: ", num print "Good bye!"

 

以上实例输出结果:

Enter a number  :20
You entered:  20
Enter a number  :29
You entered:  29
Enter a number  :3
You entered:  3
Enter a number between :Traceback (most recent call last):
  File "test.py", line 5, in <module>
    num = raw_input("Enter a number :")
KeyboardInterrupt

 

注意:以上的无限循环你可以使用 CTRL+C 来中断循环。



循环使用 else 语句

在 python 中,while … else 在循环条件为 false 时执行 else 语句块:

实例

#!/usr/bin/python count = 0 while count < 5: print count, " is less than 5" count = count + 1 else: print count, " is not less than 5"

以上实例输出结果为:

0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5


简单语句组

类似 if 语句的语法,如果你的 while 循环体中只有一条语句,你可以将该语句与while写在同一行中, 如下所示:

实例

#!/usr/bin/python flag = 1 while (flag): print 'Given flag is really true!' print "Good bye!"

标签:count,less,number,python36,Enter,print,than
From: https://www.cnblogs.com/chenyutong0321/p/16987088.html

相关文章