五、python流程控制

1. 顺序结构

2. 分支结构

2.1 if-else语句
	a = 10
	b = 20
	if a > b and a or b :
	    c = a > b and a or b
	    print(c)
2.2 if-elif-else语句
	import math
	# 输入边长
	a = int(input("请输入边长a(cm):"))
	b = int(input("请输入边长b(cm):"))
	c = int(input("请输入边长c(cm):"))
	if a < 0 or b < 0 or c < 0:
	    print("输入不合法")
	elif a + b <= c or a + c <= b or b + c <= a:
	    print("不满足条件")
	elif a - b >= c or a - c >= b or b - c >= a:
	    print("不满足条件")
	else:
	    # 求取半边长
	    p = (a + b + c) / 2
	    # 计算面积
	    s = math.sqrt(p * (p - a) * (p - b) * (p - c))
	    # 输出结果
	    print("三角形面积为:", s)
2.3 三元运算
	a = int(input("请输入数字:"))
	print("可以上网" if a > 18 else "不可以上网" )

3. 循环结构

3.1 for语句
	for i in range(10):
	    if i == 2:
	        continue  # 结束本次循环
	    if i == 5:
	        break     # 跳出循环
	    else:
	        print(i)
3.2 while语句
	# while 语句的功能是 只要condition的布尔值为True 则一直执行"do something"
	# 批量重命名,重复做一件事N次
	while condition:
		do something
	else:
		do something
	# condition 表示条件 条件的布尔值为True继续循环,布尔值为False,则结束循环
	# else 当前的while是正常循环退出时会执行else部分的代码
3.3 break continue pass
	break结束循环
	continue结束本次循环
	pass占位符
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值