python--分支语法

# 单分支语法
# if 条件表达式:
# 代码指令
# ...
score = 60
if score <= 60:
    print('成绩不理想,要继续加油')
    pass
print('语句运行结束')

# 双分支
# if 条件表达式:
#     代码指令
# else:
#     代码指令
score = 60
if score >= 60:
    print('成绩合格!')
    pass
else:
    print('成绩不合格请继续努力')

print('运行结束')

# 多分支
score = int(input('请输入你的成绩:'))
if score > 90:
    print('成绩是A等级')
    pass
elif score >= 80:
    print('成绩是B等级')
    pass
elif score >= 70:
    print('成绩是C等级')
    pass
elif score >= 60:
    print('成绩是D等级')
    pass
else:
    print('可以回家修理地球了')

print('程序执行结束')

# 循环
# 打印99乘法表
row = 1
while row <= 9:
    col = 1
    while col <= row:
        print('{}*{}={}'.format(row, col, row * col), end=' ')
        col += 1
    print()
    row += 1

# break continue
for item in 'i love python':
    if item == 'e':
        break
        # continue
    else:
        print(item)

# 打印乘法表
for i in range(1, 10):  # range i取值左闭右开区间每次自增1
    for j in range(1, i + 1):
        print('{}*{}={}'.format(i, j, i * j), end=' ')
    print()

# for --- else     /       while --- else
for item in range(1, 10):
    print(item, end=' ')
    if item >= 5:
        break
else:
    print('在本层循环当中,出现break那么else中不执行')

# 例子----输入账号密码判断是否成功登录
account = 'admin'
password = '123'
for i in range(3):
    zh = input('请输入账号:')
    mm = input('请输入密码:')
    if account == zh and password == mm:
        print('账号密码正确,登录成功!')
        break
    else:
        print('账号或密码错误,还有{}次机会'.format(2 - i))
else:
    print('账号已被锁住')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值