代码示例库

搜索、筛选并收藏你需要的 Python 代码片段。

重置
#入门 #字符串 #数字 #列表 #列表 #循环 #切片 #if #列表 #字典 #字典 #while #while #函数 #函数 #类 #类 #文件 #异常 #测试 #测试 #Pygame #Pygame #matplotlib #随机漫步 #CSV #API #Django #Django #Django

if-elif-else

第5章 if语句

演示条件判断结构。

age = 12
if age < 4:
    price = 0
elif age < 18:
    price = 10
else:
    price = 20
print(f"票价 {price} 元")

requested_toppings = ["mushrooms", "green peppers", "cheese"]
for topping in requested_toppings:
    if topping == "green peppers":
        print("抱歉,青椒没有了")
    else:
        print(f"添加 {to
...

用if处理列表

第5章 if语句

演示列表非空检查与包含判断。

requested_toppings = []
if requested_toppings:
    print("有配料")
else:
    print("没有配料,来份原味披萨")

available = ["mushrooms", "olives", "pepperoni"]
requested = ["mushrooms", "pineapple"]
for item in requested:
    if item in available:
        print(f"可以加 {item}")
    else:
        print(f"抱歉,没有 {item}
...