企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ***** # 5.1 一个简单示例 ``` if car == 'bmw': print(car.upper()) ``` ## 5.2.5 检查多个条件 **1. 使用and检查多个条件** ``` age_0 >= 21 and age_1 >= 21 ``` **2. 使用or检查多个条件** ``` age_0 >= 21 or age_1 >= 21 ``` ## 5.2.6 检查特定值是否包含在列表中 ``` requested_toppings = ['mushrooms', 'onions', 'pineapple'] 'mushrooms' in requested_toppings #True ``` ## 5.2.7 检查特定值是否不包含在列表中 ``` banned_users = ['andrew', 'carolina', 'david'] user = 'marie' if user not in banned_users: print(user.title() + ", you can post a response if you wish.") ``` ## 5.2.8 布尔表达式 game_active = True # 5.3 if 语句 ## 5.3.1 简单的 if 语句 ``` if age >= 18: print("You are old enough to vote!") ``` ## 5.3.2 if-else 语句 ``` if age >= 18: print("You are old enough to vote!") print("Have you registered to vote yet?") else: print("Sorry, you are too young to vote.") print("Please register to vote as soon as you turn 18!") ``` ## 5.3.3 if-elif-else 结构 ``` if age < 4: price = 0 elif age < 18: price = 5 elif age < 65: price = 10 elif age >= 65: price = 5 ``` ## 5.3.4 使用多个 elif 代码块 可以在最后省略else ``` if age < 4: price = 0 elif age < 18: price = 5 elif age < 65: price = 10 elif age >= 65: price = 5 ``` ## 5.4.2 确定列表不是空的 ``` requested_toppings = [] #判断列表是不是不为空 if requested_toppings: ```