Python学习入门基础教程(learning Python)--3.2 if-else分支语句 .
2013-08-28 15:41
1136 查看
if-else分支语句结构的特点是当conditon条件满足时,执行if下的语句块,当condition条件不满足时执行else下的语句块,也就是说根据条件来控制让某些语句执行,某些语句不被执行。
if-else语法结构如下:
[python]view plaincopyprint?if conditon:
(tab) statement1
(tab) statement2
(tab) statement3
etc.
else:
(tab) statement1
(tab) statement2
(tab) statement3
etc.
接下来沿用上节的例子,使用以下Python的if-else结构进行程序设计。
[python]view plaincopyprint?def input_info():
mat = int(raw_input("plz input your mathtic record: "))
eng = int(raw_input("plz input your english record: "))
his = int(raw_input("plz input your history record: "))
process_info(mat, eng, his)
def process_info(m, e, h):
ave = (m + e + h) / 3
if ave > 90:
print("Congratulation! ave = ", ave)
else:
print("Need improve! ave = ", ave)
def main():
input_info()
main()
if-else语法结构如下:
[python]view plaincopyprint?if conditon:
(tab) statement1
(tab) statement2
(tab) statement3
etc.
else:
(tab) statement1
(tab) statement2
(tab) statement3
etc.
if conditon: (tab) statement1 (tab) statement2 (tab) statement3 etc. else: (tab) statement1 (tab) statement2 (tab) statement3 etc.需要注意的是(a)在if的condition之后要有冒号':',else后边也要有一个冒号':',(2)if和else其下的每条语句都要用TAB键缩进。if-else条件控制分支语句的逻辑流程图如下图所示。
接下来沿用上节的例子,使用以下Python的if-else结构进行程序设计。
[python]view plaincopyprint?def input_info():
mat = int(raw_input("plz input your mathtic record: "))
eng = int(raw_input("plz input your english record: "))
his = int(raw_input("plz input your history record: "))
process_info(mat, eng, his)
def process_info(m, e, h):
ave = (m + e + h) / 3
if ave > 90:
print("Congratulation! ave = ", ave)
else:
print("Need improve! ave = ", ave)
def main():
input_info()
main()
def input_info(): mat = int(raw_input("plz input your mathtic record: ")) eng = int(raw_input("plz input your english record: ")) his = int(raw_input("plz input your history record: ")) process_info(mat, eng, his) def process_info(m, e, h): ave = (m + e + h) / 3 if ave > 90: print("Congratulation! ave = ", ave) else: print("Need improve! ave = ", ave) def main(): input_info() main()程序的运行结果如下图所示。
相关文章推荐
- Python学习入门基础教程(learning Python)--3.2 if-else分支语句
- Python学习入门基础教程(learning Python)--3.3.4 Python的if-elif-else语句
- Python学习入门基础教程(learning Python)--3.1Python的if分支语句 .
- Python学习入门基础教程(learning Python)--3.1Python的if分支语句
- Python学习入门基础教程(learning Python)--3.3 分支语句的条件表达式详解 .
- Python学习入门基础教程(learning Python)--1.2.1 Python输出语句print基本使用 .
- Python学习入门基础教程(learning Python)--3 Python分支结构和布尔逻辑
- Python学习入门基础教程(learning Python)--1.1Python程序设计流程
- Python新手学习基础之条件语句——if/else语句
- Python学习入门基础教程(learning Python)--3.3.2 Python的关系运算
- Python学习入门基础教程(learning Python)--1.2.4 Python格式化输出科学计数 .
- Python学习入门基础教程(learning Python)--2.2.2 Python全局和局部变量 .
- 零基础入门学习Python(34)--丰富的else语句及简洁的with语句
- Python学习入门基础教程(learning Python)--2.2.1 Python下的变量解析
- Python学习入门基础教程(learning Python)--1.2Python输入输出与处理 .
- Python学习入门基础教程(learning Python)--1.3 Python数据输入 .
- Python学习入门基础教程(learning Python)--2.2 Python下的变量基础
- Python学习入门基础教程(learning Python)--2.3 Python自定义函数传参
- Python学习入门基础教程(learning Python)--5.1 Python下文件处理基本过程
- Python学习入门基础教程(learning Python)--8.3 字典常用的方法函数介绍