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
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学习:程序控制结构·作业20141219
- Python学习之五【程序控制结构-选择结构&&循环结构】
- python初级学习之基于收发邮件的远程控制程序
- python初级学习之基于收发邮件的远程控制程序
- 3 Python - 程序结构与控制流
- Python学习手记 pt3 控制流
- JAVA基础学习(三)---程序结构与控制、函数与数组入门
- Python学习 第4天 基本语法-变量、数据类型、运算符、流程控制
- Python模块学习 ---- threading 多线程控制和处理
- Python学习笔记——字符编码与二进制,字符编码的区分和介绍和用的交互程序
- 学习python的第四十七天-第九章 异常处理与程序调试
- python 程序控制NAO机器人行走
- Python 学习笔记 - 12.流程控制(Control Flow)
- python学习初体验——如何编写和运行程序
- 一个程序学会python的流程控制
- Python学习之路,简单的注册登录小屁程序
- java学习08--程序流程控制--判断结构
- 小Y的Python学习日志--流程控制(逻辑符)
- python3.6+pyqt5 IDE: Pycahrm+Qtdesign 快速开发GUI程序学习资源
- python学习笔记五——流程控制