Python入门(八)分支与循环

分支

先例

写一个程序,按百分制评等级:
[90,100]等级为A,
[80,90)等级为B,
[60,80)等级为C,
[0,60)等级为D,
当用户输入分数时,自动转换为ABCD的形式输出。

  • rank1.py
>>> score = int ( input ('Please Input the Score:'))
if score < 0 or score > 100:
    print ('Input Error!')
if 90 <= score <= 100:
    print ('A')
if 80 < score <90:
    print ('B')
if 60 < score <=80:
    print ('C')
if 0 <= score <=60:
    print ('D')

F5运行后,输入分数:
1

  • rank2.py
>>> score = int ( input ('Please Input the Score:'))
if score < 0 or score > 100:
    print ('Input Error!')
elif 90 <= score <= 100:
    print ('A')
elif 80 < score <90:
    print ('B')
elif 60 < score <=80:
    print ('C')
elif 0 <= score <=60:
    print ('D')

F5运行后,输入分数:
2

  • rank3.py
>>> score = int ( input ('Please Input the Score:'))
if score < 0 or score > 100:
    print ('Input Error!')
else:
    if 90 <= score <= 100:
        print ('A')
    else:
        if 80 < score <90:
            print ('B')
        else:
            if 60 < score <=80:
                print ('C')
            else:
                print ('D')

F5运行后,输入分数:

3

rank1 VS rank2 VS rank3
  • rank1的特点:不论程序执行到哪一步能满足条件,都会把整个程序都走一遍,再输出结果。
  • rank2的特点:只要程序执行到满足条件的那一步,就会终止程序输出结果,比rank1节省时间。
  • rank3的特点:与rank2的执行方式一样,只是运用了python中特有的elif语句,简洁明了!
    rank2体现了python中的缩进机制,完美的避开了,在其他语言中经常会掉的坑。

悬挂else

像C语言中有如下代码:

if (a > 0)
    if (b > 0)
        printf("^_^")
else
    printf("&-&");

请问else是和哪个if搭配的呢?对和第二个if搭配!
BUT,在大量代码面前,这种框架的关键字像沧海一粟,找起来很费劲。
所以,悬挂else的问题在python中完全不存在。


条件表达式(三元操作符)
  • 买家秀:
x,y = 1,2
if x < y:
    print('min =',x)
else:
    print('min =',y)

F5运行后得:
vs1

  • 卖家秀:
x,y =1,2
print('min =', x if x < y else y)

F5运行后得:
vs1
即使得到相同的结果,过程仍可能有很大的差别!
由上述对比可知,条件表达式的语法为:

x if 条件 else y

当条件为真时,程序将返回x的值,反之,返回y的值。

断言(assert)
>>> assert 1 > 2
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    assert 1 > 2
AssertionError

由此异常可知,关键字 assert 后的条件为假时程序便会产生AssertionError,所以说,当我们需要确保程序中的某一个条件一定为真才能让程序正常工作的话时,用assert在程序中置入检查点。


循环

while 循环
while 条件:
  循环体

只要条件为真,程序就会一直执行循环体。

while 1:
    str1=str(input("你现在有什么想法:"))
    print("原来你很",str1,"呀!")

4

for 循环
for 目标 in 表达式:
  循环体

目标是指变量,表达式可为序列,可以是列表也可以是字符串。

>>> players = ['网易云音乐','酷狗音乐','QQ音乐']
>>> for player in players:
    print ('安装:',player,len(player))

    
安装: 网易云音乐 5
安装: 酷狗音乐 4
安装: QQ音乐 4

for 与很多BIF连用时会实现各种各样的功能,这里以range([star,]stop[,step=1])为例:

>>> for i in range(1,10,2):
    print(i)

    
1
3
5
7

break和continue
  • 关键字break的作用是终止当前循环,跳出循环体。
>>> for i in '123456789':
    if i == '7':
        break
    print(i)

    
1
2
3
4
5
6
  • 关键字continue的作用是终止当前循环,开始下次循环。
>>> for i in '123456789':
    if i == '7':
        continue
    print(i)

    
1
2
3
4
5
6
8
9

需要注意,循环语句可以有 else 子句,它在穷尽列表(以for循环)或条件变为 false (以while循环)导致循环终止时被执行,但循环被break终止时不执行。

>>> for i in range(1,10):
    for x in range(2,i):
        if i % x == 0:
            print(i,'能分解成',x,'*',i//x)
            break
    else:
        print(i,'是质数!')

        
1 是质数!
2 是质数!
3 是质数!
4 能分解成 2 * 2
5 是质数!
6 能分解成 2 * 3
7 是质数!
8 能分解成 2 * 4
9 能分解成 3 * 3

END!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值