Python 练习 {if} {while} {for} {break}

if操作

#!/usr/bin/env python
#!-*- coding=utf-8 -*-
#! Filename: if_statements.py
number = 23
guess = int(raw_input('Enter an integer:'))
if guess == number:
    print 'Congratulations, you guessed it.'
    print 'but you do not win any prizes!'
elif guess < number:
    print 'No, it is little higher than that'
else:
    print 'No, it is little lower than that'
    
print 'Done'

while操作

#!/usr/bin/env python
#!_*_ codng=utf-8 -*-
#!Filename: while.py
number = 23;
running = True

while running:
    guess = int(raw_input('Enter an integer:'))
    if guess == number:
        print 'Congratulations, you guessed it'
        running = False
    elif guess < number:
        print 'No, it is a little higher than that'
    else:
        print 'No, it is a little lower than that '
        
else:
    print 'The while loop is over'
    
print 'Done'

for操作

#!/usr/bin/env python
#!-*- coding=utf-8 -*-
#Filename: for.py
for i in range(1,5):
    print i
else:
    print 'The for loop is over'

break操作

#!/usr/bin/env python
#!_*_ coding=utf-8 _*_
#FileName:break.py
while True:
    s = raw_input("Enter something:")
    if s == 'quit':
        break;
    else:
        print 'Length of the string is ', len(s)
print 'Done'

gowhich说输出结果,自己copy测试一下就知道了,嘿嘿

CC BY-NC-ND 4.0