執行語句可以是單個語句或語句塊。判斷條件可以是任何表達式,任何非零、或非空(null)的值均爲true。當判斷條件假false時,循環結束。
執行流程圖如下:
name = 'python' flag = True if name == 'luna': print flag else: print name if name == 'xiaphong': print flag elif name == 'xiapong': print flag elif name == 'python': print '朋友你猜對了' else: print False
#!/usr/bin/python count = 0 while count < 100: print 'The count is:', count count = count + 1 print "Good bye!"
#!/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!"
輸出:
Python 2.7.15 |Anaconda, Inc.| (default, Dec 10 2018, 21:57:18) [MSC v.1500 64 bit (AMD64)] Type "copyright", "credits" or "license" for more information. IPython 5.8.0 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. PyDev console: using IPython 5.8.0 Python 2.7.15 |Anaconda, Inc.| (default, Dec 10 2018, 21:57:18) [MSC v.1500 64 bit (AMD64)] on win32 runfile('D:/pycharm/TEST02/python file/test02.py', wdir='D:/pycharm/TEST02/python file') Enter a number :>? 3 You entered: 傻瓜 3 Enter a number :