前言
python3当中的条件语句是非常简单简洁的,说下这两种:if 条件、while 条件。
一、if 条件语句
1.if 语句:
# 如果条件成立,打印666
if True:
print(666)
2.if...else:
a = 9
if a < 5:
print('你愁啥?')
else:
print('我是小龙')
3.if...elif...else:
a = int(input('你几岁啦?')) # str 转换 int
if a == 18:
print('美女您好哈!')
elif a == 25:
print('还行,勉强你能接受!')
else:
print('其他就不看了!')
当然还可以多层嵌套,不妨试下吧:
二、while 语句
2,break
1.无限循环,打印666
# 第一种
while True:
print(666)
# 第二种,让条件不成立退出循环
a = 1
b = True
while b:
print(a)
if a == 10:
b = False
a = a+1
2.while 条件循环
a = 1
while a <= 10:
print(a)
a = a+1
3.break:满足条件跳出整个循环 或 执行到 break 退出整个循环
a = 1
while True:
print(a)
if a == 10:
break # 满足条件跳出循环
a = a+1
4.continue:Python continue 语句跳出本次循环,而break跳出整个循环。
a = 0
while a < 5:
a += 1
if a == 3:
continue
print(a)
5.while...else:
count = 6
while count < 5:
count += 1
if count == 3:
break
print(count)
else:
print("循环出现异常!!!")
PS:
1、终止循环1,改变条件;2,使其不成立。
2、while 1 效率比 while True 高
如果您在学习python基础,不妨写个猜年龄游戏吧,粘贴在评论区吧。欢迎来QQ交流群:482713805