流程控制之if、while、for

一、if判断

1、语法一:

if 条件:
    条件成立时执行的子代码块
    代码1
    代码2
    代码3


示例:

sex='female'
age=18
is_beautiful=True

if sex == 'female' and age > 16 and age < 20 and is_beautiful:
    print('开始。。。')

print('other code1...')
print('other code2...')
print('other code3...')

2、语法二:

if 条件:
    条件成立时执行的子代码块
    代码1
    代码2
    代码3
else:
    条件不成立时执行的子代码块
    代码1
    代码2
    代码3
    
    
示例:
sex='female'
age=38
is_beautiful=True

if sex == 'female' and age > 16 and age < 20 and is_beautiful:
    print('开始。。。')

else:
    print('阿姨好。。。')                    

print('other code1...')
print('other code2...')
print('other code3...')

3、语法三:

if 条件1:
    if 条件2:
        代码1
        代码2
        代码3

示例:
sex='female'
age=18
is_beautiful=True
is_successful=True
height=1.70

if sex == 'female' and age > 16 and age < 20 and is_beautiful \
        and height > 1.60 and height < 1.80: 
    print('开始。。。')
    if is_successful:
        print('。。。')
    else:
        print('什么')
else:
    print('阿姨好。。。')

print('other code1...')
print('other code2...')
print('other code3...')

4、语法四:

if 条件1:
    代码1
    代码2
    代码3
elif 条件2:
    代码1
    代码2
    代码3
elif 条件3:
    代码1
    代码2
    代码3

else:
    代码1
    代码2
    代码3
    
示例:
如果成绩 >= 90,那么:优秀
如果成绩 >= 80且 < 90, 那么:良好 
如果成绩 >= 70且 < 80, 那么:普通
其他情况:很差

score = input('please input your score: ')  # score='100'
score = int(score)

if score >= 90:
    print('优秀')
elif score >= 80:
    print('良好')
elif score >= 70:
    print('普通')
else:
    print('很差')

二、while循环。while可以嵌套使用,以下案例有解释说明。

语法:

while 条件:
    代码1
    代码2
    代码3


例如:
while True:
    name=input('please input your name: ')
    pwd=input('please input your password: ')

    if name == 'egon' and pwd == '123':
        print('login successful')
    else:
        print('username or password error')


打印出1-10个数
count = 1
while count <= 10: print(count) count += 1

1、结束while循环的两种方式:

方式一:条件改为false:条件mag改为False。条件改为False时,不会立即结束本层循环,是在下一次循环判断条件时才会生效。

方式一:条件mag改为False。
        条件改为False时,不会立即结束本层循环,是在下一次循环判断条件时才会生效。
mag = True
while mag:
    name = input('我的暗号是:>>>')
    password = input('请输入下一句:>>>')
    if name == '请问今天你带烟了吗' and password == '我已经戒烟几年了':
        print('输入正确,你是自己人')
        while mag:
            print("""
            0 会话结束
            1 你现在正在进行的工作
            2 你的名字
            3 你今天洗澡了吗
            """)
            choice = input('你想知道的我都可以告诉你,你问吧')
            if choice == '0':
                mag = False
            elif choice == '1':
                print('你现在正在进行的工作')
            elif choice == '2':
                print('你的名字')
            elif choice == '3':
                print('你今天洗澡了吗')
            else:
                print('我不想回答')
    else:
        print('输入错误,你是间谍')

方式二:while + break:break 一定是在循环体内,当程序运行到break时,就会立刻结束本层循环。

while True:
    name = input('我的暗号是:>>>')
    password = input('请输入下一句:>>>')
    if name == '请问今天你带烟了吗' and password == '我已经戒烟几年了':
        print('输入正确,你是自己人')
        while True:
            print("""
            0 会话结束
            1 你现在正在进行的工作
            2 你的名字
            3 你今天洗澡了吗
            """)
            choice = input('你想知道的我都可以告诉你,你问吧')
            if choice == '0':
                break
            elif choice == '1':
                print('你现在正在进行的工作')
            elif choice == '2':
                print('你的名字')
            elif choice == '3':
                print('你今天洗澡了吗')
            else:
                print('我不想回答')
        breakelse:
        print('输入错误,你是间谍')

while + continue:是在循环体内结束本次循环,直接执行下一次循环。

打印出1-10个数
count = 1
while count <= 10: print(count) count += 1 打印1-5,8-10,不打印6和7 count = 1
while count <= 10: if count == 6 or count == 7: continue print(count) count += 1

三、for循环:for 循环能解决的,while循环都能解决。

但是为什么还要使用for循环呢,是因为在特定的情况下,使用for循环更简便一些。

它的强大之处在于能循环取值。

l = ['a', 'b', 'c', 'd', 'e']
i = 0while i < len(l):
    print(l[i])
    i += 1         
# 结果为
b
c
d
e


name = ['jerry', 'tommy', 'anne', 'una']
for x in name:
    print(x)

1、for + break

name = ['jerry', 'tommy', 'anne', 'una']
for x in name:
    if x == 'anne':
        break
    print(x)

2、for + continue

name = ['jerry', 'tommy', 'anne', 'una']
for x in name:
    if x == 'anne':
        continue
    print(x)

3、for + else

name = ['jerry', 'tommy', 'anne', 'una']
for x in name:
    if x == 'zoe':
        break
    print(x)
else:
    print('其他')

4、for + range

for + range   #  顾头不顾尾
for x in range(0, 5):
    print(x)  # 01234

5、for 嵌套:顾头不顾尾。range(1,5) 取值为 1 2 3 4

for x in range(1, 4):
    for y in range(2, 5):
        print(x, y)    
# 结果为
1 2
1 31 42 22 32 43 23 33 4

四、练习

1、实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败!

name = input('请输入用户名:>>>')
password = input('请输入密码:>>>')
if name == 'seven' and password == '123':
    print('登录成功')

else:
    print('登录失败')

2、实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次

count = 1
while count < 4:
    name = input('请输入用户名:>>>')
    password = input('请输入密码:>>>')
    if name == 'seven' and password == '123':
        print('登录成功')
        break
    else:
        print('登录失败')
        count += 1

3、求1-2+3-4+5 ... 99的所有数的和

res = 0
count = 1
while count <= 99:
    if count % 2 == 0:
        res -= count
    else:
        res += count
    count += 1
print(res)

4、使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12

count = 1
while count <= 12:

    if count == 6 or count == 10:
        count += 1
        continue
    print(count)
    count += 1

5、使用 while 循环实现输出 1-100 内的所有奇数

count = 1
while count <= 100:
    if count % 2 == 1:
        print(count)
    count += 1

使用 while 循环实现输出 1-100 内的所有奇数

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

6、用户登陆(三次机会重试)

count=0
while count < 3:
    name=input('请输入用户名:')
    password=input('请输入密码:')
    if name == 'egon' and password == '123':
        print('login success')
        break
    else:
        print('用户名或者密码错误')
        count+=1

7、猜年龄游戏

age_of_oldboy=73

count=0
while count < 3:
    guess=int(input('>>: '))
    if guess == age_of_oldboy:
        print('you got it')
        break
    count+=1

8、猜年龄游戏升级版

要求: 允许用户最多尝试3次 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序 如何猜对了,就直接退出

age_of_oldboy=73

count=0
while True:
    if count == 3:
        choice=input('继续(Y/N?)>>: ')
        if choice == 'Y' or choice == 'y':
            count=0
        else:
            break

    guess=int(input('>>: '))
    if guess == age_of_oldboy:
        print('you got it')
        break
    count+=1
原文地址:https://www.cnblogs.com/zhangguosheng1121/p/10572255.html