if语句在for循环中的使用
在对列表中的每一个元素进行处理时,if语句可以提供条件。
cars = ["toyota","bmw","sangtana","jili"]
for car in cars:
if car == "bmw":
print(car.upper())
else:
print(car.title())
- 条件测试
# 检查是否相等
car = "bmw"
car == "bmw"
# 不区分大小写
car = "Audi"
car.lower() == "audi"
# 检查是否不相等
vegetable = "mashrooms"
vegetable != "tomato"
# 多条件检查
age = 19
age>0 and age<20
# 特定值是否在列表中
cars = ["toyota","bmw","sangtana","jili"]
"bmw" in cars
# 特定值是否不在列表中
cars = ["toyota","bmw","sangtana","jili"]
"audi" not in cars
- 布尔表达式
是条件测试的别称
# if语句
age = 19
if age >= 18:
print("You can vote.")
# if-else语句
age = 19
if age >=18:
print("You can vote.")
else:
print("you cannot")
# if-elif-else 结构
age = 12
if age > 19:
print("you can vote")
elif age>12 and age<=19:
print("you can try")
else:
print("you cannot")
# 测试多个条件
# 在可能有多个条件为True,且你需要在每个条件为True时都采取相应措施时,适合使用这种方法。
# 总之,如果你只想执行一个代码块,就使用if-elif-else结构;如果要运行多个代码块,就使用一系列独立的if语句。但是最后一个不要用else
# 5-3
alien_colour = "green"
if alien_colour == "green":
print("you have 5 point")
# 未通过
alien_colour = "red"
if alien_colour == "green":
print("you have 5 point")
# if-else
alien_colour = "green"
if alien_colour == "green":
print("you have 5 points")
else:
print("you have 10 points")
# if-else_2
alien_colour = "red"
if alien_colour == "green":
print("you have 5 points")
else:
print("you have 10 points")
# if-elif-else
alien_colour = input()
if alien_colour == "green":
print("you have 5 points")
elif alien_colour == "yellow":
print("you have 10 points")
else:
print("you have 15 points")
# 5-6
age = int(input())
if age<2:
print("you are a baby")
if age>=2 and age<4:
print("you learn to walk")
if age>=4 and age<13:
print("you are a child")
if age>=13 and age<20:
print("you are a young man")
if age>=20 and age<65:
print("you are an adult")
if age>=65:
print("you are an old man")
- 使用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 right now.")
else:
print("Adding " + requested_topping + ".")
print("\nFinished making your pizza!")
# 确定列表非空
requested_toppings = []
if requested_toppings:
for requested_topping in requested_toppings:
print("Adding " + requested_topping + ".")
print("\nFinished making your pizza!")
else:
print("Are you sure you want a plain pizza?")
# 我们进行了简单检查,而不是直接执行for循环。在if语句中将列表名用在条件表达式中时,Python将在列表
#至少包含一个元素时返回True,并在列表为空时返回False。如果requested_toppings不为空,就
#运行与前一个示例相同的for循环;否则,就打印一条消息,询问顾客是否确实要点不加任何配
#料的普通比萨。
# 使用多个列表
available_toppings = ['mushrooms', 'olives', 'green peppers','pepperoni', 'pineapple', 'extra cheese']
requested_toppings = ['mushrooms', 'french 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-8 特殊打招呼
users =["mia","admin","lewis","gracie"]
for user in users:
if user == "admin":
print("Hello admin, would you like to see a status report?")
else:
print("Hello "+ user+", thank you for logging in again")
# 处理没有用户的情形
users =[]
if users:
for user in users:
if user == "admin":
print("Hello admin, would you like to see a status report?")
else:
print("Hello "+ user+", thank you for logging in again")
else:
print("We need to find some users!")
# 检查用户名:按下面的说明编写一个程序,模拟网站确保每位用户的用户名
# 都独一无二的方式
current_users = ["mia","admin","lewis","gracie"]
new_users = ["mia","gracie","martin","bomi"]
for user in new_users:
if user in current_users:
print(user.title()+", you are cueerntly have your account")
if user not in current_users:
print(user.title()+", you can reg your account")
# 序数:序数表示位置,如 1st 和 2nd。大多数序数都以 th 结尾,只有 1、2 和 3例外。
nums=[1,2,3,4,5,6,7,8,9]
for num in nums:
if num == 1:
print(str(num)+"st")
elif num == 2:
print(str(num)+"rd")
else:
print(str(num)+"th")