raw_input,if-elif-else,while
raw_input
1 print "hello world" 2 3 name=raw_input("what is your name? ") 4 print "hello ["+name +"] !" 5 6 raw_input("press <enter> to close")
raw_input 过滤空值
1 name='' 2 while not name or name.isspace(): 3 name=raw_input('type in your name: ') 4 if name=='a': 5 print "a" 6 elif name=='b': 7 # none 8 pass 9 elif name=='c': 10 print 'ok' 13 print("-----------------------") 15 raw_input("enter to exit ")
1 age= input('input your age ') 2 print("you are "+str(age)) 3 if age<=30: 4 print("you are younger than me") 5 elif 31<=age<=50: 6 print("between 31 and 50") 7 else: 8 print("you are older enough") 9 10 print("done")
for
1 words=['one','two','three'] 2 for word in words: 3 print word 4 print "-----------------------------" 5 6 #到下标但不包含 7 for number in range(1,5): 8 print number 9 10 print "-----------------------------" 11 #输出为 12 1 13 2 14 3 15 4
while 注意使用时避免死循环
1 x=1 2 while x<=3: 3 print x 4 x+=1 5 6 print 'done' 7 8 9 name='' 10 while not name or name.isspace(): 11 name=raw_input('type in your name: ') 12 print 'hello . %s ' % name
1 #始终打印用户输入的值 只到什么也不输入了 2 print "------------while true-----------" 3 4 while True: 5 word=raw_input('type in one word: ') 6 if not word:break 7 print 'The word is '+word