Python 编程技术4.Python流程控制-

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

while_stmt ::= "while" expression ":" suite ["else" ":" suite]
This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the ``else`` clause, if present, is executed and the loop terminates.
>>>Help() Help>if Help>elif
The ``if`` statement is used for conditional execution:
if_stmt ::= "if" expression ":" suite ( "elif" expression ":" suite )* ["else" ":" suite]
例题4.2: level="B"
elifscore>=60: level="C"
>>> print("yoursco根re:",sc据ore,"yo考urlev试el:",lev成el) 绩将成绩分为A,B,C,D四档。
yourscore:78 yourlevel:B
4. Python 流程控制
9
>>> x=int(input("pleaseinputxaixssize:x=")) >>> y=int(input("please inputyaixssize:y=")) >>> ifx>=0: ify>=0: print("坐标(%i,%i),在第一象限" %(x,y))
Help(random) %(int(kk),int(NHuemlsp)-(ii)r) andom.randint)
else:
print("唉!输入的数字太小:%i,剩下次数:%i,继续努力哦!"%(int(kk),int(Nums)-ii))
else:
print("唉!你已经猜满次数:%i,下次继续努力哦!" %(int(Nums)))
注: elif 和 else 都是可选的,elif 可有多个,作用等同 于C/C++ 中的 switch / case 语句。
思考题:如何解决多个 case的判断问题,一个解决方案 是使用 for 循环。
4. Python 流程控制
5
help> if The ``if`` statement ********************
It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true (see section *Boolean operations* for the definition of true and false); then that suite is executed (and no other part of the ``if`` statement is executed or evaluated). If all expressions are false, the suite of the ``else`` clause, if present, is executed.
例题4.3: else:
print("坐标(%i,%i),在第四象限" %(x,y)) else:
输入( x , y ),判断属于第几象限。 ify>=0:
print("坐标(%i,%i),在第二象限" %(x,y)) else: print("坐标(%i,%i),在第三象限" %(x,y))
坐标(3,-3),在第四象限
>>>Help() Help>print
提示:1.使用三个判断,也可使用判断的嵌套; 2. print语句的最后一个参数后加end=‘’,即可不换
行; 3. print语句可使用格式化字符串; 4. 可直接使用汉字,大约12行程序,约235字节。
4. Python 流程控制
10
>>> x=int(input("pleaseinputxaixssize:x=")) >>> y=int(input("please inputyaixssize:y=")) >>> ifx>=0: ify>=0: print("坐标(%i,%i),在第一象限" %(x,y)) else: print("坐标(%i,%i),在第四象限" %(x,y)) else: ify>=0: print("坐标(%i,%i),在第二象限" %(x,y)) else: print("坐标(%i,%i),在第三象限" %(x,y))
坐标(3,-3),在第四象限
4. Python 流程控制
11
4.2 while 循环语句
While循环语句while/break/continue/else
while <test>:
#Loop test
<statements1> #Loop body
i f condition:
break
else:
Related help topics: TRUTHVALUE
4. Python 流程控制
6
Python语言比较运算符
>>>Help() Help>is Help>in
4. Python 流程控制
7
>>> pass_level=60
>>> score=input("please inputyourscore:") please inputyourscore:45 >>> ifint(score)>=pass_level: print("pass")
e l i f <test2>: #condition 2 test
<statements2> # t est 2 i s true, statements 2
else:
#conditions 1 and 2 are fa lse
<statements3> # t est 1、2 i s false,statements 3
print("true")
条件语句代码块通过缩进对齐表达代码逻辑而
eprlisnet(:"f不alse")是使用大括号,因为没有了额外的字符,程序
false 的可读性更高。
>>>
■ 一是简洁;
■ 二是可读性好。
>>>Help() Help>if Help>elif
i f <test1>: #condition 1 test <statements1> # t est 1 i s true , statements 1
4. Python 流程控制
12
help> while The ``while`` statement ***********************
>>>Help() Help>while
The ``while`` statement is used for repeated execution as long as an expression is true:
这是可以通过 Python 控制语句来实现的。在 Python中有三种控制语句——i f 条件语句、for 和while循环语句。
4. Python 流程控制
3
>>> ifTrue: print("true")
4.1 i f 条件语句 else:
print("false")
true
I f >>>ifFalse:
4. Python 流程控制
16
4.3 for 循环语句
for 循环语句特点
for 循环在 Python 中是一个通用的序列迭代器, 可以遍历任何有序的序列对象内的元素。
for 语句可用于字符串、列表、元组、其他内置 可迭代对象,以及用户通过类创建的新对象。
for <target> in <object>:#Assign object items to target <statements> #Repeated loop body:use target
ii+=1
kk=input("请输入猜想的数字:")
ifint(kk)== guess:
print("恭喜你!猜对的数字:%i,猜对的次数:%i,不要骄傲哦!"%(guess,ii))
break elifint(kk)> guess:
print("唉!输入的数字太大:%i,剩下次数:%i,继续努力哦!"
else:
print("唉!你已经猜满次数:%i,下次继续努力哦!" %(int(Nums)))
4. Python 流程控制
14
4. Python 流程控制
15
import random
guess=random.randint(1,100)
ii=0
Nums=input("请输入猜想的次数:")
whileii<int(Nums):
A ``break`` statement executed in the first suite terminates the loop without executing the ``else`` clause"s suite. A ``continue`` statement executed in the first suite skips the rest of the suite and goes back to testing the expression.
continue
else:
#Optional else
<statements2> #Run i f didn"t e x i t loop with break
while进行循环控制,它对表达式进行测试,如果 为真,则循环执行循环体。
如果测试为假,则会执行else块。如果循环被中断 (break),则else语句块不会执行。
4. Python 流程控制
13
import random
guess=random.randint(1,100)
ii=0
Nums=input("请输入猜想的次数:")
whileii<int(Nums):
ii+=1
例题4.4: kk=input("请输入猜想的数字:")
ifint(kk)== guess: print("恭喜你!猜对的数字:%i,猜对的次数:%i,不要骄傲哦!"%(guess,ii))
Python 编程技术 4. Python 流程控制
4. Python 流程控制
1 . i f 条件语句 2. while 循环语句句 3. for 循环语句 4. 其它常用语句
4. Python 流程控制
2
控制语句简介 在到目前为止我们所见到的程序中,总是有一
系列的语句,Python忠实地按照它们的顺序执 行它们。如果想要改变语句流程的执行顺序, 该怎么办呢? 例如,想要让程序做一些决定, 根据不同的情况做不同的事情,如:根据时间 打印“早上好”或者“晚上好”等。
例题4.1: else:
print("fail")
fail
输入成绩,判断是否通过考试。
4. Python 流程控制
8
>>> score=int(input("please inputyour score:")) please inputyourscore:78 >>> ifscore>=80:
level="A" elifscore>=70:
4. Python 流程控制
4
Байду номын сангаас
i f 条件语句格式: i f / e l i f / else
1 . i f 条件1:
2.
表达式1
3 . e l i f 条件2:
4.
表达式2
5. else:
6.
表达式3
#条件1为真需要执行的语句段 #条件1为假,条件2为真执行的语句段 #条件1和2全部为假时执行的语句段
break elifint(kk)
>
gues猜s: 数字游戏,5次机会猜1-100内的数字。
print("唉!输入的数字太大:%i,剩下次数:%i,继续努力哦!" %(int(kk),int(Nums)-ii))
else:
print("唉!输入的数字太小:%i,剩下次数:%i,继续努力哦!"%(int(kk),int(Nums)-ii))
相关文档
最新文档