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测试一下就知道了,嘿嘿
版权声明
由 davidzhang创作并维护的 Gowhich博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证。
本文首发于 博客( https://www.gowhich.com ),版权所有,侵权必究。
本文永久链接: https://www.gowhich.com/blog/445
版权声明
由 davidzhang创作并维护的 Gowhich博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证。
本文首发于 Gowhich博客( https://www.gowhich.com ),版权所有,侵权必究。
本文永久链接: https://www.gowhich.com/blog/445