2.python条件语句与循环

1.概述

通过条件语句来判断,条件成立执行某些代码,条件不成立则不执行这些代码

2.if语句

if条件:
	条件成立执行的代码
	......
下方代码没有缩进到if语句块,所以和if条件无关

if…else

if条件:
	条件成立执行的代码
	......
else:
	条件不成立执行的代码
userName = input('请输入账号:')
password = input('请输入密码:'if userName == '张三' and password == '123456':
    print('登入成功')
else:
    print('账号或密码错误')
  
age = int(input('请输入您的年龄:'))
if age < 18:
    print(f'年龄为{age},不可上网')

多重判断

if条件:
	条件1成立执行的代码
	......
elif 条件2:
	条件2成立执行的代码
else:
	条件不成立执行的代码
age = int(input('请输入您的年龄:'))
print(f'您的年龄为{age}', end = '')
if age < 18:
    print('童工')
elif 18 <= age <= 60:
    print('正常工')
else:
    print('退休工')

if嵌套

if 条件1:
	条件1成立执行的代码
	if 条件2:
		条件2成立执行的代码 

3.三目运算符

三目运算符也叫三元运算符或三元表达式

条件成立执行的表达式 if 条件 else 条件不成立执行表达式

案例:猜拳游戏

your = int(input('请出拳:0-石头,1-剪刀,2-布'))
computer = random.randint(0, 2)
if computer == 0:
    if your == 0:
        print('平局', end='')
    if your == 1:
        print('你输了', end='')
    if your == 2:
        print('你赢了', end='')
if computer == 1:
    if your == 1:
        print('平局', end='')
    if your == 2:
        print('你输了', end='')
    if your == 0:
        print('你赢了', end='')
if computer == 2:
    if your == 2:
        print('平局', end='')
    if your == 0:
        print('你输了', end='')
    if your == 1:
        print('你赢了', end='')
computerStr = '石头' if computer == 0 else '剪刀' if computer == 1 else '布'
print(f'电脑为{computerStr}')

4.while循环

循环的作用:让代码更高效的重复执行
循环的分类:在Python中,循环分为while和for两种,最终实现效果想通过

while 条件:
	条件成立重复执行的代码

案例: 1-100累加

a = 1
total = 0
while a <= 100:
    total += a
    a += 1
print(total)

案例:1-100偶数和

a = 1
total = 0
while a <= 100:
    if a % 2 == 0:
        total += a
    a += 2
print(total)

break: 当某些条件成立退出整个循环

a = 10
while a < 100:
    a += 1
    if a == 20:
        break
print(a)

continue: 退出当前一次循环而执行下一次循环
注: 如果使用continue,在continue之前一定要修改金属漆,否则进入死循环

a = 10
total = 0
while a <= 100:
    if a % 10 == 0:
        a += 1
        continue
    total += a
    a += 1
print(total)

while循环嵌套

while 条件1:
	条件1成立执行的代码
	while 条件2:
		条件2成立执行的代码

案例,每个月有4周,每周上5天班,打印一个月的上班情况

week = 1
while week <= 4:
    day = 1
    while day <= 7:
        if day >= 6:
            day += 1
            continue
        print(f'第{week}周的星期{day}')
        day += 1
    week+= 1

案例:打印正方形

line = 0
while line < 5:
    col = 0
    while col < 5:
        print('*', end='')
        col += 1
    line += 1
    print()

案例:打印三角形

while line < 5:
    col = 0
    while col <= line:
        print('*', end='')
        col += 1
    line += 1
    print()

案例:九九乘法表

line = 1
while line <= 9:
    col = 1
    while col <= line:
        print(f'{col}*{line}={col*line}', end=',')
        col += 1
    line += 1
    print()

5.for循环

for 临时变量 in 序列
	重复执行的代码
str1 = 'hello word'
for str in str1:
    if str == 'w':
        break
    if str == 'l':
        continue
    print(str)

6.else

循环可以和else 配合使用,else下方缩进的代码指的是当循环正常结束之后要执行的代码

i = 0
while i < 10:
    print(i)
    i += 1
    if i == 5:
        # 执行else
        continue
    if i == 6:
        # 不执行else
        break
else:
    print('else执行')
print('正常执行')
i = '123456789'
for num in i:
    print(num)
    if i == '4':
        # els 执行
        continue
    if i == '6':
        # else不执行
        break
else:
    print('else执行')
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值