banned_users =['andrew','carolina','advid']
user ='marie'if user notin banned_users:print(user.title()+", you can post a response if you wish.")
结果为
5.2.8 布尔表达式
通常用于记录文件
game_active =True
can_edit =False
5.3 if 语句
5.3.1 简单的if 语句
* 格式:
if conditional_test:
do something
age =19if age >=18:print("YOU are old enough to vote!")print("Have you registered to vote yet?")
结果为
5.3.2 if-else 语句
age =17if 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 结构
age =12if age <4:
price =0elif age <18:
price =5else:
price =10print("Your admission cost is $"+str(price)+".")
结果为
5.3.4 使用多个elif代码块
age =54if age <4:
price =0elif age <18:
price =5elif age <65:
price =7.5else:
price =10print("Your admission cost is $"+str(price)+".")
结果为
5.3.5 省略 else 代码块
可以使用if - elif -elif-elif … 结构,末尾的else可省略
5.3.6 测试多个条件
使用多个 if 并列 条件语句,他们同等关系由缩进TAB键确定,
也可以使用if - elif -elif-… 语句 使其连接,只能有一条满足条件并执行一条结果
5.4 使用 if 语句处理列表
requested_toppings =['mushrooms','green peppers','extra cheese']for requested_topping in requested_toppings :if(requested_topping =='green peppers'):print("Sorry,we are out of green peppers rightnow.")else:print("Adding "+ requested_topping +".")print("\n Finished making your pizza")
结果为
5.4.2 确定列表不是空的
requested_toppings =[]# 判断列表是否为空if requested_toppings:for requested_topping in requested_toppings :print("Adding "+ requested_topping +".")else:print("are you sure you want a plain pizza?")
结果为
5.4.3 使用多个列表
available_toppings =['mushrooms','olives','green pppers','pepperoni','pineapple','extra cheese']
requested_toppings =['mushrooms','fresh fries','extra cheese']for requested_topping in requested_toppings:if requested_topping in available_toppings:print("Adding "+ requested_topping +".")else:print("Sorry,we don't have "+ requested_topping +".")
5.1 一个简单的示例cars = ['audi','bmw','subaru','toyota']for car in cars: if car == 'bmw': print(car.upper()) else: print(car.title())结果为5.2 条件测试5.2.1 检查是否相等>>> car = 'bmw'>>&...