if、while、for循环

一.if判断

1.if...else

cls = 'human'
gender = 'female'
age = 38

if cls == 'human' and gender == 'female' and age >16 and age < 22:
    print('表白开始')
else:
    print('阿姨好')
阿姨好

2.if...elif...else

cls = 'human'
gender = 'female'
age = 28

if cls == 'human' and gender == 'female' and age > 16 and age < 25:
    print('开始表白')
elif cls == 'human' and gender == 'female' and age > 25 and age < 30:
    print('考虑下')
else:
    print('阿姨好')
考虑下

3.if嵌套

cls = 'human'
gender = 'female'
age = 18
is_success = False

if cls == 'human' and gender == 'female' and age > 16 and age < 22:
    print('开始表白')
    if is_success:
        print('那我们一起走吧...')
    else:
        print('我逗你玩呢')
else:
    print('阿姨好')
开始表白
我逗你玩呢

练习

engo_age=73
age=int(input('请猜年龄:'))
if age==engo_age:
    print('猜对了')
elif age>engo_age:
    print('猜大了')
elif age<engo_age:
    print("\033[1;31;43m猜小了\033[0m")
请猜年龄:4
[1;31;43m猜小了[0m
engo_age=73
age=int(input('请输入年龄:'))
if age!=engo_age:
    if age>engo_age:
        print('猜大了')
    else:
        print('猜小了')
else:
    print('猜对了')
请输入年龄:511
猜大了

二.while循环

1.while+break

while True:
    engo_age=73
    age=int(input('请输入年龄:'))
    if age!=engo_age:
        if age>engo_age:
            print('猜大了')
        else:
            print('猜小了')
    else:
        print('猜对了')
        break
请输入年龄:45
猜小了
请输入年龄:73
猜对了

break的意思是终止当前循环,执行其他代码

2.while+continue

n = 1
while n < 10:
    if n == 8:
        n += 1   # 如果注释这一行,则会进入死循环
        continue
    print(n)
    n += 1
1
2
3
4
5
6
7
9

continue的意思是终止本次循环,直接进入下一次循环

3.while循环的嵌套

while True:
    user_db = 'xiaoming'
    pwd_db = '123'

    inp_user = input('username:')
    inp_pwd = input('password:')

    if inp_user == user_db and pwd_db == inp_pwd:
        print('login successful')
        while True:
            cmd = input('请输入你需要的命令:')
            if cmd == 'q':
                break
            print(f'{cmd}功能执行')
        break
    else:
        print('username or password error')
print('退出了while循环')
username:xiaoming
password:123
login successful
请输入你需要的命令:q
退出了while循环

三.for循环

1.for+break

name_list = ['xiaoming','mark','tony','nini']
for name in name_list:
    if name == 'tony':
        break
    print(name)
xiaoming
mark

2.for+continue

name_list = ['xiaoming','mark','tony','nini']
for name in name_list:
    if name == 'mark':
        continue
    print(name)
xiaoming
tony
nini

3.for循环嵌套

for i in range(3):
    print(f'---:{i}')
    for j in range(2):
        print(f'***:{j}')
---:0
***:0
***:1
---:1
***:0
***:1
---:2
***:0
***:1

练习1:实现loading加载效果

import time
print('loading',end=' ')
for i in range(6):
    print('.',end=' ')
    time.sleep(1)
loading . . . . . .

练习2:不改变列表地址,把列表内容颠倒

s=['h','e','l','l','o']
for i in range(len(s)//2):
    s[i],s[len(s)-i-1]=s[len(s)-i-1],s[i]
print(s)
['o', 'l', 'l', 'e', 'h']

转载于:https://www.cnblogs.com/gaohuayan/p/10907911.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值