本文目录导读:

我来给你详细讲解Python中嵌套if的多层判断方法和案例。
基础语法结构
if condition1:
if condition2:
if condition3:
# 执行代码
else:
# 条件3不成立
else:
# 条件2不成立
else:
# 条件1不成立
实用案例
案例1:学生成绩评级系统
# 学生成绩评级(包含作弊和补考判断)
score = 85
is_cheat = False
is_retake = True
if score >= 60: # 第一层:是否及格
if is_cheat: # 第二层:是否作弊
print("成绩作废,记为0分")
else:
if score >= 90: # 第三层:成绩等级
print("优秀")
elif score >= 80:
print("良好")
elif score >= 70:
print("中等")
else:
print("及格")
else: # 不及格的情况
if is_retake: # 第二层:是否可以补考
print("需要补考")
else:
print("需要重修")
案例2:超市会员折扣系统
# 超市购物折扣计算
total_amount = 500
is_member = True
member_level = "gold" # silver, gold, platinum
birthday_month = True
if is_member: # 第一层:是否为会员
print("会员用户,可以享受折扣")
if member_level == "platinum": # 第二层:会员等级
if birthday_month: # 第三层:特殊优惠
final_amount = total_amount * 0.6 # 6折
print(f"铂金会员生日月:{final_amount}元")
else:
final_amount = total_amount * 0.7 # 7折
print(f"铂金会员:{final_amount}元")
elif member_level == "gold":
if birthday_month:
final_amount = total_amount * 0.7
print(f"黄金会员生日月:{final_amount}元")
else:
final_amount = total_amount * 0.8
print(f"黄金会员:{final_amount}元")
else: # silver
if birthday_month:
final_amount = total_amount * 0.85
print(f"白银会员生日月:{final_amount}元")
else:
final_amount = total_amount * 0.9
print(f"白银会员:{final_amount}元")
else: # 非会员
if total_amount > 300: # 第二层:非会员满减
print(f"非会员满300减30:{total_amount - 30}元")
else:
print(f"非会员无优惠:{total_amount}元")
案例3:登录验证系统
# 多层登录验证
username = "admin"
password = "123456"
is_locked = False
login_attempts = 0
if not is_locked: # 第一层:账户是否被锁定
if username == "admin": # 第二层:用户名验证
if password == "123456": # 第三层:密码验证
print("登录成功!")
login_attempts = 0 # 重置尝试次数
else:
login_attempts += 1
if login_attempts >= 3: # 第四层:尝试次数判断
print("密码错误次数过多,账户已锁定!")
is_locked = True
else:
print(f"密码错误,还剩{3-login_attempts}次机会")
else:
print("用户名不存在")
else:
print("账户已被锁定,请联系管理员")
案例4:天气出行建议
# 根据天气和温度建议出行
weather = "rainy" # sunny, cloudy, rainy, snowy
temperature = 25
have_umbrella = True
have_car = False
if weather == "rainy": # 第一层:天气类型
print("下雨天气")
if have_umbrella: # 第二层:雨具检查
if have_car: # 第三层:交通工具
if temperature > 20: # 第四层:温度判断
print("开车出行,记得带伞")
else:
print("开车出行,注意保暖")
else:
if temperature > 20:
print("步行或公交出行,带好雨伞")
else:
print("步行或公交出行,注意防寒和带伞")
else:
print("先买把伞再出门吧!")
elif weather == "snowy":
print("下雪天气")
if temperature < 0:
if have_car:
print("开车出行,注意防滑")
else:
print("建议乘坐公共交通,注意保暖和防滑")
else:
print("带好雨具,小心路滑")
else: # sunny or cloudy
print("天气不错")
if temperature > 35:
print("高温天气,注意防暑")
elif temperature > 25:
print("适合出行")
else:
print("建议添加外套")
优化建议
- 避免过度嵌套:嵌套超过3层时,代码可读性会变差
- 使用and/or连接条件:简化多层判断
# 不推荐:多层嵌套
if condition1:
if condition2:
if condition3:
do_something()
# 推荐:使用and连接
if condition1 and condition2 and condition3:
do_something()
- 提前返回:减少嵌套层次
def login_check(username, password):
if not username:
return "用户名不能为空"
if not password:
return "密码不能为空"
if username != "admin":
return "用户名不存在"
if password != "123456":
return "密码错误"
return "登录成功"
练习题
尝试完成这个嵌套判断:
# 电影票购买系统 age = 25 is_student = True watch_3d = True have_coupon = False # 根据条件计算票价: # 1. 儿童(<12岁):免费 # 2. 学生(12-22岁):半价 # 3. 成人(22-60岁):全价 # 4. 3D电影加收10元 # 5. 有优惠券减15元
这样你就可以从简单的例子开始,逐步掌握Python嵌套if的使用方法了。