python if 语句,pythonif,#!/usr/bin/p


#!/usr/bin/python## python使用random模块生成随机数## import random模块,import表示导入模块import random# 生成在10,50范围内的随机数i = random.randrange(10,50)print 'Your number is', i# if语句if i < 20:    print "That is less than 20."    if i % 3 == 0:        print "It is divisible by 3."elif i == 20:    print "That is exactly twenty.  How nice for you."else:    if i % 2 == 1:        print "That is an odd number."    else:        print "That is twice", i / 2, '.'    print "Wow! That's more than 20!"

注意python很巧妙,它不需要用{}来表示语句块,语句块是通过缩进来表示的。

if i<20:之后是if语句的内容,该if语句块的语句必须比if多一个缩进,其内部的if i%3==0" 是内部if语句的开始,同样它下面的语句也要比它多一个缩进;而在if结束开始elif时,elif的缩进和if是平齐的,同时也表示上面的if块结束了。

评论关闭