***************************************** CODE*****************************************
''' Char 5 IF '''
cars=['audi','bmw','subaru','toyota']
for car in cars:
if car=='bmw':
print(car.upper())
else:
print(car.title())
# ~ 相等运算符== 在它两边的值相等时返回True ,否则返回False
car='audi' # ❶
car=='bmw' # ❷
# ~ 一个等号是陈述;对于❶处的代码,可解读为“将变量car 的值设置为'audi' ”。
# ~ 两个等号是发问;对于❷处的代码,可解读为“变量car 的值是'bmw' 吗?”。大多数编程语言
# ~ 使用等号的方式都与这里演示的相同。
car='Audi'
if car.lower()=='audi':
print("\n this car is audi, it is true\n")
# ~ 检查是否不相等 !=
re='mushrooms'
if re != 'annnnn':
print("hold the list\n")
# ~ 比较数字
age=20
if age==18:
print("this guy is pretty young.\n")
else:
print("er.....not 18 yet\n")
if age != 50:
print("this is not the correct answer. please try again!\n")
if age > 10:
print("age is over 10.\n")
# ~ 检查多个条件 and or
location='toronto'
age=22
if location=='toronto' and age >= 20:
print("this guy comes from toronto and he is allowed to drink beer.\n")
if location!="london" and age >= 20:
print("this guy is not coming from toronto but he is allowed to drink here\n")
if age
print("sorry, you can't order any drink.\n")
if location=="toronto" or age
print("toronto's student?? needs his/her ID to verify\n")
# ~ 检查特定值是否包含在列表中
stu_list=['d','e','t','a']
if 'd' in stu_list:
print("d in the list!\n")
# ~ 检查特定值是否不包含在列表中
banned_users=['chang','gao','zhou','wang']
user='hao'
if user not in banned_users:
print("Hi " + user.title() + ", we'll grant you normal access.\n")
else:
print("sorry, we can't grant you access. pls contact local admin\n")
# ~ 布尔表达式
# ~ 随着你对编程的了解越来越深入,将遇到术语布尔表达式 ,它不过是条件测试的别名。与条件表达式一样,
# ~ 布尔表达式的结果要么为True ,要么为False 。
# ~ 布尔值通常用于记录条件,如游戏是否正在运行,或用户是否可以编辑网站的特定内容:
car = 'subaru'
print("\nIs car == 'subaru'? I predict True.")
print(car == 'subaru')
print("\nIs car == 'audi'? I predict False.")
print(car == 'audi')
#try;
#5-2
loca1='toronto'
loca2='london'
localist=['freiBUrg','Beijing','LONDON','shanghai','TORONTO']
ageone=22
agetwo=60
if loca1=='toronto':
print("location is toronto, this is ")
print(loca1=="toronto")
if ageone >= 20 and agetwo
print("this guy could drink maximum 500ml beer per day.\n")
if ageone = 60:
print("- you can't drink according to your age")
trip="shanghai"
if trip not in localist:
print("\nsorry, pls try another location, we can't find it in our list\n")
else:
print("\nok plesse check in here\n")
tt="kk"
if tt != "kkk":
print("error\n")
# if 语句; P46
yard=1900
if yard >=2000:
print("\nyou are our VIP")
print('are you registed to this meeting?\n')
else:
print("sorry, you are currently on our waiting list.\n")
'''经常需要检查超过两个的情形,为此可使用Python提供的if-elif-else 结构'''
'''Python并不要求if-elif 结构后面必须有else 代码块'''
''' 0-18 19-59 others;'''
ticket=16
if 0
print("this ticket is free for you.\n")
elif ticket
print("this ticket is $10.\n")
else:
print("this ticket is $5.\n")
# ~ or
if ticket
print("this ticket is free for you.\n")
elif ticket
print("this ticket is $10.\n")
else:
print("this ticket is $5.\n")
# ~ or
if ticket
price="free"
elif ticket
price="$10"
else:
price="$5"
print("your ticket cost is " + price + ".")
'''测试多个条件 if '''
requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
print("\nadding mushrooms.\n")
if 'pepperoni' in requested_toppings:
print('\nadding pepperoni.\n')
if 'extra cheese' in requested_toppings:
print('\nadding extra cheese.\n')
#try:
#5-3:
alien_color='red'
if 'green' in alien_color:
gains=5
print("congras, your just got " + str(gains) + " points.")
if 'red' in alien_color:
gains=5
print("congras, your just got " + str(gains) + " points.")
# 5-4
if 'green' in alien_color:
gains=5
print("congras, your just got " + str(gains) + " points.")
else:
gains=10
print("congras, your just got " + str(gains) + " points.")
# 5-5;
if 'green' in alien_color:
gains=5
elif 'yellow' in alien_color:
gains=10
elif 'red' in alien_color:
gains=15
print("congras, your just got " + str(gains) + " points.")
#5-6
age=65
if age
print("\nthis is a baby.\n")
elif age
print("this is a toddler.\n")
elif age
print("this is a kid.\n")
elif age
print("this is a young.\n")
elif age
print("this is an adult.\n")
else:
print("this is a senior citizen.\n")
''' 使用if 语句处理列表'''
#通过创建一个列表,在其中包含顾客点的配料
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for toppings in requested_toppings:
print("adding " + toppings + ".")
print('\nfinished making your pizza!\n')
#然而,如果比萨店的青椒用完了,该如何处理呢?为妥善地处理这种情况,可在for 循环中包含一条if 语句:
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for toppings in requested_toppings:
if toppings=='green peppers':
print('sorry, we are out of green peppers')
else:
print("adding " + toppings + ".")
print('\nfinished making your pizza!\n')
#确定列表不是空的
requested_toppings=[]
if requested_toppings:
for toppings in requested_toppings:
print("adding " + toppings + ".")
else:
print("\nare you sure you want a plain pizza?\n")
# 使用多个列表
'''下面的示例定义了两个列表,其中第一个列表包含比萨店供应的配料,而第二个列表包含顾客点的配料。这次对
于requested_toppings 中的每个元素,都检查它是否是比萨店供应的配料,再决定是否在比萨中添加它:'''
available_toppings = ['mushrooms', 'olives', 'green peppers',
'pepperoni', 'pineapple', 'extra cheese']
requested_toppings = ['mushrooms', 'french fries', 'extra cheese']
for order in requested_toppings:
if order in available_toppings:
print('adding ' + order + '.')
else:
print('\nsorry, we dont have ' + order + '.')
print("\nFinished making your pizza!")
#try:
#5-8 5-9
name=['zhang','wang','li','zhao','hou']
check=[]
if name:
for check in name:
if 'zhang' in check:
print('Hello Zhang, would you like to see a staus report?\n')
else:
print('Hello ' + check.title() + ', thank you for looging in again.')
else:
print('\nWe need to find some users!\n')
#5-10
current_users=['zhang','wang','li','zhao','hou']
new_users=['hang','ZHOU','LI','wang','gao']
if new_users:
for check in new_users:
if check.lower() in current_users:
print('it is our existed user.\n')
else:
print('sorry, we can not find this user\n')
else:
print('\nSorry, please list the new user.\n')
#5-11
numlist=list(range(1,11))
for check in numlist:
if check==1: print('1st')
elif check==2: print('2nd')
elif check==3: print('3rd')
else: print(str(check) + 'th')
'''在本章中,你学习了如何编写结果要么为Ture 要么为False 的条件测试。你学习了如何编写简单的if
语句、if-else 语句和if-elif-else 结构。在程序中,你使用了这些结构来测试特定的条件,以确定这些
条件是否满足。你学习了如何在利用高效的for 循环的同时,以不同于其他元素的方式对特定的列表元素进行处理。你还再次学习了Python就代码格式方面提出的建议,这可确保即便你编写的程序越来越复杂,其代码依然易于阅读和理解。'''
领取专属 10元无门槛券
私享最新 技术干货