执行语句可以是单个语句或语句块。判断条件可以是任何表达式,任何非零、或非空(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 :