003、Python-流程控制(if 判断、while、for循环)

流程控制

# 执行流程的分类
	1、顺序结构
  	自上而下依次执行
  	2、分支结构
  	在运行过程中,不同的条件会执行不同的流程
  	3、循环结构
  	再满足判断条件之后会执行循环体内的代码,当循环体内的代码执行完之后,会再次返回到判断条件,若依旧满足,则会再次执行循环体内的代码

分支结构——if

1、单分支 if

  age = 48
  if age >38 :
	 print('阿姨好')

2、if/else 分支

  age = 48
  if age >38 :
    print('阿姨好')
  else :
    print('小姐姐好')

3、if/else/elif 分支

	username="franklin"
  if username == 'jason':		#条件 1
    print('ceo')						#条件 1成立执行
  elif username == 'elin':	#条件 2
    print('cfo')						#条件 1不成立,条件 2 成立执行
  elif username == 'alex':	#条件 3
    print('waiter')					#条件 1、2 不成立,条件 3 成立执行
  else :										#条件 4
    print('no message')			#条件 1、2、3 都不成立时执行

4、if的嵌套

在这里插入图片描述

	age = 22
  is_success  = True
  if age < 38:
    print('hello 靓女加个微信')
    if is_success:
      print('美好的一天')
    else :
      print('就这?')
  else :
    print('认错人了')

循环结构——while、for

1、while 循环

	"""
	语法结构
		while 条件:
			条件成立之后执行的循环体代码
	1.先判断条件是否成立 如果成立则执行循环体代码
	2.循环体代码执行完毕后 再次判断条件是否成立 如果成立则继续执行循环体代码 如果不成立则跳出循环
	"""

2、while+break

```python
"""
break 用于结束所处的层数的循环
"""
	while True:
    print('123')
    while True:
      break
      print('6666')
      
   # 结果为  123 123 123 123 ...
 while True:
    print('123')
    while True:
      print('6666')
      break
      
   # 结果为  123 6666 123 6666 123 6666 123 6666 ...
```

3、while+continue

```python
"""
continue 用于直接跳过本次循环,continue之后的代码不执行,并且直接跳到循环的判断条件处
"""
				count = 1
        while count < 11:
            if count == 4:
                count += 1
                continue  # 结束本次循环 开始下一次循环
                '''直接跳到条件判断的地方重新执行'''
            print(count)
            count += 1  
          # 结果不打印  4  
       count = 1
        while count < 11:
            if count == 4:
                # count += 1
                continue  
            print(count)
            count += 1  
            # 结果只打印  1 2 3
```

4、while+else

```python
"""
当 while 循环没有被人为中断(break、continue)的情况下才会执行 else 后的代码
"""
	count = 0
	while count < 5:
    print(count)
    count += 1
  else:
    print('嘿嘿嘿')
  # 会执行 else
  
  count = 0
	while count < 5:
    	if count == 3:
        break
    print(count)
    count += 1
  else:
    print('嘿嘿嘿')
  # 不会执行 else
```

小练习

"""
编写一个猜年龄的游戏
		基本要求
    	如果不对的情况下可以猜三次 如果对了直接结束
    拔高练习
    	三次机会用完之后提示用户是否继续猜测 如果用户输入了y
        则再给用户三次机会 如果用户输入n则直接结束
"""        
Age = 19
count = 0
while count < 3:

    age = int(input('age:'))
    if age == Age :
        print('bingo')
        break
    else:
        print('猜错了')
        if count == 2:
            choice = input('输入 y,再来三次机会; n-->exit')
            if 'y' == choice:
                count = -1      # count 要自增 1,所以在+1 之后要变成 0 才有三次机会
            if 'n' == choice:
                break
        count += 1

5、for 循环

"""
for 循环能做到的事情 while 都能做到
但是 for 语法更加简洁,循环取值更加方便
语法结构:
	for 变量名 in 可迭代对象:
		循环体代码
"""
list = [111,222,333,"4444"]
count = 0
while count <5:
  print(list[count])
  count += 1
# while

list = [111,222,333,"4444"]

for index in list:
  print(index)
# for

#可迭代对象:字符串、列表、字典、元组、集合
# 字典用 for 循环取到的是 key
  

6、***range 方法

# 配合 for 循环使用
# 1、一个参数--> 从 0 开始,顾头不顾尾
for i in range(10):
  print(i)
  # 0 1 2 3 4 5 6 7 8 9
# 2、两个参数-->自定义起始位置,顾头不顾尾 
for i in range(6,10):
  print(i)
  # 6 7 8 9
# ***3、三个参数-->第三个参数用来控制步长
for i in range(2,100,40):
  print(i)
  # 2 42 82

range 方法在 Python2 中会产生一个列表,xrange 就是Python3 中一样的迭代器

range 方法在 Python3 中会产生一个迭代器

小练习

# 九九乘法表
for i in range(1,10):
  for j in range(1,i+1):
    print('%s*%s=%s' % (i,j,i*j), end='') 	
  print()	#每次小循环结束则换行
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值