第五章 if语句(所有习题答案请自己输入,不要粘贴复制)
习题备忘,以便于后续学习
练习5-1:条件测试 编写一系列条件测试,将每个测试以及对其结果的预测和实际结果打印出来。你编写的代码应类似下面这样:
car = 'subaru'
print("Is car == 'subaru'? I predict True.")
print(car == 'subaru')
car = 'subaru'
print("\nIs car == 'audi'? I predict False.")
print(car == 'audi')
- 详细研究实际结果,直到你明白它为何为True 或False.
car = 'subaru'
print("Is car == 'subaru'? I predict True.")
print(car == 'subaru')
结果为true
car = 'subaru'
print("\nIs car == 'audi'? I predict False.")
print(car == 'audi')
结果为false
- 创建至少10个测试,且其中结果分别为True和False 的测试至少有5个。
car = 'aa'
print("Is car == 'aa'? I predict True.")
print(car == 'aa')
结果为true
car = 'aa'
print("Is car == 'bb'? I predict false.")
print(car == 'bb')
结果为false
练习5-2:更多的条件测试 你并非只能创建10个测试。如果想尝试做更多比较,可再编写一些测试,并将它们加入 conditional_tests.py 中。对于下面列出的各种情况,至少编写两个结果分别为true 和false 的测试。
检查两个字符串想等和不等。
car = 'subaru'
if 'subaru' == car:
print("true")
else:
print("false")
car = 'subaru'
if 'bmw' != car:
print("false")
else:
print("true")
使用方法lower()的测试。
car = 'BMW'
if car.lower() == 'bmw':
print("true")
else:
print("false")
涉及相等,不等,大于,小于,大于等于和小于等于的数值测试。
age=15
if age == 15:
print('age =15')
else:
print('age !=15')
使用关键字and和or 的测试
age=15
if age >3 and age <=16:
print('true')
else:
print('false')
age=15
if age >18 or age <=16:
print('true')
else:
print('false')
测试特定的值是否包含在列表中
age = [15,8]
if 15 in age:
print('true')
else:
print('false')
测试特定的值是否未包含在列表中
age = [15,8]
if 13 not in age:
print('true')
else:
print('false')
练习5-3:外星人颜色 假设在游戏中刚射杀了一个外星人,请创建一个名为alien_color的变量,并将其赋值为‘green','yellow'或’red'.
编写一条if 语句,检查外星人是否是绿色的。如果是,就打印一条消息,指出玩家获得5分。
alien_color =['green']
if 'green' in alien_color:
print(f"you just earned 5 poitns!")
编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一版本中未通过(未通过测试时没有输出)。
alien_color =['green']
if 'blue' in alien_color:
print(f"you just earned 5 poitns!")
练习5-4:外星人颜色2 像练习5-3那样设置外星人的颜色,并编写一个if-else 结构。
如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了5分。
如果外星人不是绿色的,就打印一条消息,指出玩家获得了10分
编写这个程序的两个版本,在一个版本中执行if 代码块,
alien_color =['red']
if 'green' in alien_color:
print(f"you just earned 5 points!")
if 'green' not in alien_color:
print(f"you just earned 10 points")
在另一个版本中执行else代码块。
alien_color =['red']
if 'green' in alien_color:
print(f"you just earned 5 points!")
else:
print(f"you just earned 10 points")
练习5-5 外星人颜色3 将练习5-4 中if-else 结构改为if-elif-else 结构他。
如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了5分。
如果外星人是黄色的,就打印一条消息,指出玩家因射杀该外星人获得了10分。
如果外星人是红色的,就打印一条消息,指出玩家因射杀该外星人获得了15分。
编写这个程序的三个版本,分别在外星人为绿色,黄色和红色时打印一条消息。
alien_color =['red']
if 'green' in alien_color:
print(f"you just earned 5 points!")
elif 'yellow' in alien_color:
print(f"you just earned 10 points")
else:
print(f"you just earned 15 points")
练习5-6: 人生的不同阶段 设置变量age 的值,再编写一个if-elif-else 的结构,根据age 的值判断一个人处于人生的哪个阶段。
如果年龄小于2岁,就打印一条消息,指出这个人是婴儿。
如果年龄为2(含)~4岁,就打印一条消息,指出这个人是幼儿。
如果年龄为4(含)~13岁,就打印一条消息,指出这个人是儿童。
如果年龄为13(含)~20岁,就打印一条消息,指出这个人是青少年。
如果年龄为20(含)~65岁,就打印一条消息,指出这个人是成年人。
如果年龄超过65岁,就打印一条消息,指出这个人是老年人。
age = 15
if age < 2 :
title = 'infancy'
elif age < 4:
title = 'preschooler'
elif age < 13:
title = 'children'
elif age < 20:
title = 'adolescent'
elif age <65:
title = 'adult'
else:
title = 'old people'
print(f"The person in a {title}.")
练习5-7 喜欢的水果 创建一个列表,其中包含你喜欢的水果,再编写一系列独立的if 语句,检查列表中是否包含特定的水果。
将该列表命名为favorite_fruits ,并在其中包含三种水果。
编写5条if 语句,每条都检查某种水果是否包含再列表中。如果是,就打印一条消息,下面是一个例You really like bananas
favorite_fruits = ['bananas','apple','pear']
if 'bananas' in favorite_fruits:
print(f"you really like bananas!")
if 'apple' in favorite_fruits:
print(f"you really like apple!")
if 'pear' in favorite_fruits:
print(f"you really like pear")
if 'orange' in favorite_fruits:
print(f"you really like orange")
if 'watermalon' in favorite_fruits:
print(f"you really like watermalon")
练习5-8:以特殊方式跟管理员打招呼 创建一个至少包含5个用户的列表,且其中一个用户名为‘admin'.想像你要编写代码,在每位用户登录网站后打打印一条问候消息。遍历列表,并向每位用户打印一条问候消息。
如果用户名为’admin',就打印一条特殊的问候消息,如下所示:
Hello admin, would you like to see a status report?
否则,打印一条普通的问候消息,如下所示:
Hello jaden, thank you for logging in again.
users = ['admin','molly','john','bob','lucy']
for user in users:
if user == 'admin':
print(f"Hello admin,would you like to see a status report?")
else:
print(f"hello {user},thank you for logging in again")
练习5-9:处理没有用户的情形 在为完成练习5-8编写的程序中,添加一条if 语句,检查用户列表是否为空。
如果为空,就打印如下消息。
We need to find some users!
删除列表中的所有用户名,确定将打印正确的消息。
users = []
if users:
for user in users:
if user == 'admin':
print(f"Hello admin,would you like to see a status report?")
else:
print(f"hello {user},thank you for logging in again.")
else:
print("We need find some users")
练习5-10:检查用户名 按下面的说明编写一个程序,模拟网站如何确保每位用户的用户名都独一无二。
创建一个至少包含5个用户的列表,并将其命名为current_users.
再创建一个包含5个用户名的列表。将其命名为new_users,并确保其中的有一两个用户名也包含再列表current_users中。
遍历列表new_users,对于其中的每个用户名,都检查它是否已被使用。如果是,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指出这个用户名为被使用。
确保比较时不区分大小写。换句话说,如果用户名‘john' 已被使用,应拒绝用户名'JOHN'.(为此,需要创建列表current_users 的副本,其中包含当前所有的用户名的小写版本。)
current_users = ['john','molly','Harish','lucy','Tom']
new_users = ['anthony','harish','tom','marry','kuba']
for new_user in new_users:
if new_user.title() in current_users:
print(f"the {new_user} had been used")
else:
print(f"you can use the {new_user}.")
练习5-11:序数 序数表示位置,如1st和2nd.序数大多以th结尾,只有1,2,和3 例外。
在一个列表中存储书数字1~9
遍历这个列表
在循环中使用一个if-elif-else 结构,打印每个数字的对应的序数。输出内容应为”1st,2nd,3rd,4th,5th,6th,7th,8th,9th",但每个序数都独占一行。
numbers = ['1','2','3','4','5','6','7','8','9']
for number in numbers:
if number == '1':
print('1st')
elif number == '2':
print('2nd')
elif number == '3':
print('3rd')
else:
print(f"{number}th")