标签:style blog http color os art
1.if语句(python中没有switch语句,可用if……elif……else代替)
格式:
if 表达式:
#语句
else:
#语句
eg:
number = 23 guess = int(raw_input(‘Enter an integer:‘)) if guess == number: print‘Congratulations‘ print‘but you do not win any prizes!‘ elif guess < number: print‘No,it is higher‘ else: print‘NO,it is lower‘ print‘Done‘
2.while语句
格式:
while <test>:
#语句
else:#可跟else从句
eg:
1 number = 23 2 running = True 3 4 while running: 5 guess = int(raw_input(‘Enter an integer:‘)) 6 if guess == number: 7 print‘Congratulations‘ 8 running = False 9 elif guess < number: 10 print‘it is higher‘ 11 if guess > number: 12 print‘it is lower‘ 13 else: 14 print‘The while loop is over.‘ 15 16 print‘Done‘
3.for语句格式:
for <var> in range(<begin>,<end>):
#语句
1 for i in range(1,5): 2 print i 3 else: 4 print "The for loop is over"
标签:style blog http color os art
原文地址:http://www.cnblogs.com/shijianupc/p/3798912.html