您的位置:首页 > 编程语言 > Python开发

python学习-程序流控制

2012-12-19 14:57 387 查看
1 ,python 对缩进要求很严格,但可以使用空格,TAB,双空格进行缩进,一旦选择 一中缩进模式,应该始终保持。

2,注释

""单行注释

""单行注释

''' ''' 多行注释

########################################################

if流控

#!/usr/bin/python

A = 23

B = 32

if A > B :

print A , 'is bigger than B'

else :

print A , 'is not bigger than B '

#################

#!/usr/bin/python

answer = raw_input("dou you like python")

if answer == 'yes':
print 'python can most ,can do more for you'
elif answer == 'no':
print 'why do not you like it'

####################################################
for 流控

[root@qqdserver python]# for i in $(seq 1 10);do echo $i ; done
1
2
3
4
5
6
7
8
9
10

########

[root@qqdserver python]# python
Python 2.4.3 (#1, Jun 18 2012, 08:55:23)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> for i in range(1,10):
... print i #注意此处一定要有缩进
...
1
2
3
4
5
6
7
8
9 #注意这里只会输出到9

##########
for流控中嵌套if

#!/usr/bin/python

Name_list = ['zhangsan','ZHANGSAN']

for name in Name_list :
if name == 'zhangsan':
print 'hey,I know you zhangsan'
if name == 'ZHANGSAN':
print 'zhangsan is ZHANGSAN'
print 'hello,' , name
else:
print 'bye bye'

####

[root@qqdserver python]# ./for_name.py
hey,I know you zhangsan
hello, zhangsan
zhangsan is ZHANGSAN
hello, ZHANGSAN
bye bye

###################
while循环

#!/usr/bin/python

process_status = 'alive'

while True:
input = raw_input('your change')
process_status = input
if process_status == 'alive':
print 'this is aliving.....'
else:
print 'dead......'
break

##########################################

最后练习一个小程序shopping
让用户输入工资
打印出来商品和价格
计算用户是否可以支付
输出用户所剩余的钱
如若不够,输出内容

#!/usr/bin/python

salary = int(raw_input('input your salary'))

while True:
print '''shoping list
Car 200000
iphone 5000
ipad 3000'''

order = raw_input('what dou you want buy')
if order == 'Car':
if salary > 20000:
print 'great , now you have the Car \n'
salary = salary - 20000
print 'now you have %s left ' % salary
else :
print 'sorry , you can not buy the Car ,try other something %s ' % order
if order == 'iphone':
if salary > 5000:
print 'great , now you have the iphone \n'
salary = salary - 5000
print 'now you have %s left ' % salary
else :
print 'sorry , you can not buy the iphone ,try other something %s ' % order

if order == 'ipad':
if salary > 3000:
print 'great , now you have the ipad \n'
salary = salary - 3000
print 'now you have %s left ' % salary
else :
print 'sorry , you can not buy the ipad ,try other something %s ' % order
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python