05 控制流程之if判断与while、for循环

一、if判断

1.什么是if判断?

    接收用户输入的名字

    接受用户输入的密码

    如果用户输入的名字=正确的名字 并且 用户输入的密码=正确的密码

    告诉用户登录成功

    否则,

    告诉用户登录失败

2.为何要有if判断?

   为了让计算机像人一样去判断

3.如何用if判断?

    完整的语法:

   

   4、if判断基本运行原理解析(同上图)
5、分支结构
5.1.单分支
age=19
is_beautiful=True
gender="male"
if gender=="male" and 18<age<27 and is_beautiful:
    print("我喜欢你")

   5.2.双分支

 

age=19
is_beautiful=True
gender="male"
if gender=="male" and 18<age<27 and is_beautiful:
    print("我喜欢你")
else:
    print("我们不合适")
age=19
is_beautiful=True
gender="male"
if gender=="male" and 18<age<27 and is_beautiful:
    print("我喜欢你")
else:
    print("我们不合适")
 

案列

inp_user = input("请输入您的用户名:")
inp_pwd = input("请输入您的密码:")
if inp_user == "egon" and inp_pwd == "123":
    print("登录成功")
else:
    print("登录失败")

 

       5.3.多分支

如果:成绩>=90,那么:优秀
如果成绩>=80且<90,那么:良好
如果成绩>=70且<80,那么:普通
其他情况:很差
score = input("请输入您的分数")
score = int(score)
if score >= 90:
    print("优秀")
elif score >= 80:
    print("良好")
elif score >= 70:
    print("普通")
else:
    print("很差")
二、while循环语法:(条件循环)
 while 条件:
        代码1
        代码2
        代码3

1.基本用法

print('start...')
i = 1
while i <= 5:
     print(i)
     i += 1
print('end...')

2.死循环:永不结束的循环


while True:
      print(1)

while True:  
    name = input(">> ")
    print(name)

3.结束while循环有两种方式
  方式一:把条件改为False, 必须要等到下一次循环判断条件时才能结束循环

 

print('start...')
i = 1

tag = True
while tag:
    if i == 5:
        tag = False
    print(i)
    i += 1

 

 方式二:break终止本层循环,会立即结束while循环,根本没有下一次
print('start...')
i = 1

tag = True
while tag:
    if i == 5:
        break
    print(i)
    i += 1

案例1.

方法一:

tag = True
while tag:
    inp_user = input("请输入您的用户名:")
    inp_pwd = input("请输入您的密码:")

    if inp_user == "egon" and inp_pwd == "123":
        print("登录成功")
        tag = False
    else:
        print("登录失败")

方法二:

while True:
     inp_user = input("请输入您的用户名:")
     inp_pwd = input("请输入您的密码:")

     if inp_user == "egon" and inp_pwd == "123":
         print("登录成功")
         break
     else:
         print("登录失败")
案例2
i = 0
while True:
    inp_user = input("请输入您的用户名:")
    inp_pwd = input("请输入您的密码:")
    if inp_user == "egon" and inp_pwd == "123":
        print("登录成功")
        break
    else:
        print("登录失败")
        i += 1
    if i == 3:
        print("尝试次数超过了3次,结束")
        break

补充;

while True:
    while True:
        while True:
            break
        break
    break



tag = True
while tag:
    while tag:
        while tag:
            tag = False

4.while+continue: continue终止本次直接进入下一次

i = 1
while i <= 5:
    if i == 3:
        i += 1
        continue
    print(i)
    i += 1
强调:
1、不要在continue后加与continue同级的代码,加了就永远运行不了了
2、循环体代码的最后一步不要写continue
5.while+else
else的子代码何时运行:
while循环结束后并且是在正常情况下结束的,else的子代码块才会运行
ps:只要不是被break干掉的循环,都是正常结束的
i = 0
while i <= 10:
    if i == 5:
        break
    print(i)
    i += 1
else:
    print("==============")

三、for循环(取值循环)

1.for更擅长遍历值,for循环的次数取决值的个数
ums = [111, 222, 333, 444, 555]

i = 0
while i < len(nums):
     print(nums[i])
     i += 1

for num in nums:  
    print(num)

for x in "hello":
print(x)


dic = {'k1':111,'k2':222,'k3':333}
for k in dic:
print(k,dic[k])


items = [['name', "egon"], ['age', 18], ['gender', "male"]]
for x,y in items: # x,y = ['name', 'egon']
print(x,y)

2.for+break

 nums = [111, 222, 333, 444, 555]
 for num in nums:
     if num == 333:
        break
     print(num)

 

3.for+continue

 nums = [111, 222, 333, 444, 555]
 for num in nums:
     if num == 333:
         continue
     print(num)

 

4.for+else

 nums = [111, 222, 333, 444, 555]
 for num in nums:
     if num == 333:
         # break
         continue
     print(num)
 else:
     print('======')

 

5.(1)让某段代码重复运行3次-》while循环实现如下

 i = 0
 while i < 3:
     print('hello1')
     print('hello2')
     print('hello3')
     i += 1

 

  (2)让某段代码重复运行3次-》for循环实现如下

 for x in range(3):
     print('hello1')
     print('hello2')
     print('hello3')
for x in range(6):
    print(x)

6.for+enumerate


nums = [111, 222, 333, 444, 555]

 i = 0
 while i < len(nums):
     print(i,nums[i])
     i += 1


for i,num in enumerate(nums):
    print(i,num)

 

 
 
 

 



 

 
posted @ 2021-07-30 16:20  甜甜de微笑  阅读(221)  评论(0)    收藏  举报