Python课程回顾(day04)
流程控制之“if”判断
1.什么是if判断?
if判断是判断一个条件是否成立,若成立则执行哪些事,不成立则执行哪些事。
2.为什么要使用if?
因为我们需要让计算机能够像人类一样具有判断能力。
3.如何使用if判断?if的几种语法运用?
使用if之前要先给出相应的变量
语法一:给出变量,直接使用if判断条件及成立之后执行什么。
age=18
if age == 18:
print(‘ 成年 ’)
语法二:if与else,else是在if判断的条件不成立后所执行的事。
age=18
if age == 18:
print(‘ 成年 ’)
else(‘ 未成年或大于18 ’)
语法三:if中可以再次嵌套多个if,但必须要上个if条件是成立的情况下才可以执行下个if条件。而后面的if条件下还会有子代码,是建立在前两个if条件都成立的情况下才会执行,所以不可同一行编写。
age=18
name=KK
if age == 18:
if name == ‘KK’:
print(‘hello’)
print(‘那个人跟我朋友很像’)
语法四:if与elif。elif,是用于同一类型的条件下判断,例如成绩判断、同一物品价格判断、多个人年龄判断等。不属于if下的子代码。
if score >= 90:
print('优秀')
elif score >= 80:
print('良好')
elif score >= 70:
print('普通')
else:
print(‘较差’)
while循环
1.什么是循环?
循环就是重复的做某件事。
2.为什么要使用循环?
因为我需要计算机重复的去做某件事。
3.怎样使用while循环?
while循环又称之为条件循环,循环的次数取决条件是否一直为True。条件为false则循环次数为0
while循环的方式:
方式一:while后直接添加条件,再添加子代码即可。
while True: name=input(‘请输入你的名字: ’) pwd=input(‘请输入密码: ’) if name = ‘KK’ and pwd = ‘12345’ print(‘登陆成功’) else: print(‘密码错误’) (然后重新输入,无论结果正确,一直循环)
方式二:结束while循环
1.操作while的条件使其不成立
tag=True while tag: name=input(‘请输入你的名字: ’) pwd=input(‘请输入密码: ’) if name = ‘KK’ and pwd = ‘12345’ print(‘登陆成功’) tag=false (当tag等于false时,上面的while tag即等于while false,条件不成立则终止循环) else: print(‘密码错误’)
2.使用break强行终止循环
while True:
name=input(‘请输入你的名字: ’) pwd=input(‘请输入密码: ’) if name = ‘KK’ and pwd = ‘12345’ print(‘登陆成功’) break (强行终止本层循环,但不影响其他层的循环) else: print(‘密码错误’)
3.continue 代表结束本次循环,直接进入下一次循环。即代码未执行完毕时若使用continue则重新开始循环当前while层
count=1
while count < 6:
if count == 4:
count+=1
continue #从本层开始跳到while层重新开始循环
print(count)
count+=1
4.while,else (while中的else会在while循环没有被break终止的情况下执行)
count=1
while count < 6:
if count == 4:
break
print(count)
count+=1
else:
5.输入三次则退出的while+else应用
count=0
while count < 3:
name=input('请输入您的用户名: ')
pwd=input('请输入您的密码: ')
if name == 'kk' and pwd == '12345':
print('登陆成功')
break
else:
print('用户名或密码不正确')
count+=1
6.while循环的嵌套。while循环内可分层次的嵌套多个while循环。
name='kk'
pwd='12345'
count=0
while count < 3:
name=input('请输入您的账号密码: ')
pwd=input('请输入您的密码: ')
if name == 'kk' and pwd == '12345':
print('succesful')
while True:
print('''
1 浏览商品
2 添加购物车
3 支付
4 退出
''')
choice = input('请输入您想要的商品')
if choice == '1':
print('开始浏览商品')
elif choice == '2':
print('开始添加购物车')
elif choice == '3':
print('正在支付...')
elif choice == '4':
break
break
else:
print('用户名或密码错误')
count+=1
7tag控制所有while循环,先将while后的条件赋值变量为tag,再将tag变为False,即所有while后的tag都为False,循环终止!
count=0
while count < 3:
name=input('请输入您的账号密码: ')
pwd=input('请输入您的密码: ')
if name == 'kk' and pwd == '12345':
print('succesful')
tag=True
while tag:
print('''
1 浏览商品
2 添加购物车
3 支付
4 退出
''')
choice = input('请输入您想要的商品')
if choice == '1':
print('开始浏览商品')
elif choice == '2':
print('开始添加购物车')
elif choice == '3':
print('正在支付...')
elif choice == '4':
tag=Flase
break
else:
print('用户名或密码错误')
count+=1
for循环
for循环主要应用在取值,相比while循环取值更简便快捷。for循环取值是将列表或字典内的值,依次变量赋值并打印。满足用户条件后自动结束循环。
stu={'name1':
{'age':19,'sex':'female'} ,
'name2':{'age':17,'sex':'female'},
'name3':{'age':20,'sex':'female'}
}
for i in stu:
if i == 'name1':
print(stu[i])

浙公网安备 33010602011771号