第五章:if 语句
示例
并以首字母大写的方式打印其中的汽车名,但对于汽车名’bmw’ ,以全大写的方式打印
cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())
输出:
Audi
BMW
Subaru
Toyota
检查特定值是否包含在列表中
>>> requested_toppings = ['mushrooms', 'onions', 'pineapple']
>>> 'mushrooms' in requested_toppings
True
>>> 'pepperoni' in requested_toppings
False
检查特定值是否不包含在列表中
>>> requested_toppings = ['mushrooms', 'onions', 'pineapple']
>>> 'pepperoni' not in requested_toppings
True
if-elif-else 结构
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
else:
price = 10
print("Your admission cost is $" + str(price) + ".")
省略else 代码块
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
elif age >= 65:
price = 5
print("Your admission cost is $" + str(price) + ".")
注意: else 是一条包罗万象的语句,只要不满足任何if 或elif 中的条件测试,其中的代码就会执行,这可能会引入无效甚至恶意的数据。如果知道最终要测试的条件,应考虑使用一个elif 代码块来代替else 代码块。这样,你就可以肯定,仅当满足相应的条件时,你的代码才会执行。
确定列表不是空的
在if 语句中将列表名用在条件表达式中时,Python将在列表至少包含一个元素时返回True ,并在列表为空时返回False 。
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?")
使用多个列表
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 + ".")
print("\nFinished making your pizza!")
输出:
Adding mushrooms.
Sorry, we don't have french fries.
Adding extra cheese.
Finished making your pizza!
习题
5-2
car = 'Audi'
if car == 'Audi':
print("This is an Audi car.")
elif car == 'bmw':
print("This is not an Audi car.")
print("---------------------------------------------")
print(car == 'audi')
print(car.lower() == 'audi')
print("---------------------------------------------")
number = 50
print(number == 35)
print(number != 35)
print(number > 35)
print(number >= 35)
print(number < 35)
print(number <= 35)
print(number > 35 and number < 40)
print(number > 35 or number < 35)
print("--------------------------------------------")
cars = ['audi', 'bmw', 'toyota', 'subaru']
if 'honda' in cars:
print("本田在列表中")
if 'honda' not in cars:
print("本田不在列表中")
5-3
#版本1 测试通过
alien_color = 'green'
if alien_color == 'green':
print("You get five points")
#版本2 测试未通过
alien_color = 'red'
if alien_color == 'green':
print("You get five points")
5-4
#版本1 仅使用if
alien_color = 'yellow'
if alien_color == 'green':
print("You get five points")
if alien_color == 'red':
print("You get ten points")
if alien_color == 'yellow':
print("You get ten points")
#版本2 使用if-else
alien_color = 'yellow'
if alien_color == 'green':
print("You get five points")
else:
print("You get ten points")
5-5
alien_color = 'red'
if alien_color == 'green':
print("You get five points")
elif alien_color == 'yellow':
print("You get ten points")
else :
print("You get fifteen points")
5-6
age = 65
if age < 2:
print("婴儿")
elif age < 4:
print("蹒跚学步")
elif age < 13:
print("儿童")
elif age < 20:
print("青少年")
elif age < 65:
print("成年人")
else:
print("老年人")
5-7
favorite_fruits = ['apple', 'peach', 'banana']
test_fruits = ['apple', 'pear', 'strawberry', 'peach', 'banana']
for test_fruit in test_fruits:
if test_fruit in favorite_fruits:
print("You really like " + test_fruit + "!")
5-8
users = ['admin', 'Tom', 'Eric', 'Jack', 'Bruce']
for user in users:
if user == 'admin':
print("Hello " + user + ",would you liketo see a status report?")
else:
print("Hello " + user + ",thank you for logging in again.")
5-9
users = []
if users:
for user in users:
if user == 'admin':
print("Hello " + user + ",would you liketo see a status report?")
else:
print("Hello " + user + ",thank you for logging in again.")
else:
print("We need to find some users!")
5-10
current_users = ['Tom', 'JACK', 'Bruce', 'JERRY', 'Sam']
new_users = ['david', 'Jack', 'jerry', 'Smith','John']
current_users2=[]
for current_user in current_users:
current_users2.append(current_user.title())
for new_user in new_users:
if new_user.title() in current_users2:
print(new_user.title() + " has been used!You need to input other usernames")
else:
print(new_user.title() + " has not been used.")
5-11
numbers = list(range(1, 11))
for number in numbers:
print(number)
print("-------------------------------")
for number in numbers:
if number == 1:
print(str(number) + "st")
elif number == 2:
print(str(number) + "nd")
elif number == 3:
print(str(number) + "rd")
else:
print(str(number) + "th")
1301

被折叠的 条评论
为什么被折叠?



