练习5-1
条件测试 编写一系列条件测试,将每个测试以及对其结果的预测和实际结果打印出来。你编写的代码应类似于下面这样:
car = ‘subaru’
print(“Is car == ‘subaru’? I predict True.”)
print(car == ‘subaru’)
print(“\nIs car == ‘audi’? I predict False.”)
print(car == ‘audi’)
- 详细研究实际结果,直到你明白它为何为True 或False。
- 创建至少10个测试,且其中结果分别为True 和False 的测试都至少有5个。
car = 'subaru'
print("Is car == 'subaru'? I predict True.")
print(car == 'subaru')
print("\nIs car == 'audi'? I predict False.")
print(car == 'audi')
food = 'cake'
print("Is food == 'cake'? I predict True.")
print(food == 'cake')
print("\nIs car == 'pizza'? I predict False.")
print(food == 'pizza')
...
输出:
Is car == 'subaru'? I predict True.
True
Is car == 'audi'? I predict False.
False
Is food == 'cake'? I predict True.
True
Is car == 'pizza'? I predict False.
False
...
练习5-2
更多条件测试 你并非只能创建10个测试。如果想尝试做更多比较,可再编写一些测试,并将它们加入conditional_tests.py中。对于下面列出的各种情,至少编写两个结果分别为True 和False 的测试。
- 检查两个字符串相等和不等。
- 使用方法 lower() 的测试。
- 涉及相等、不等、大于、小于、大于等于和小于等于的数值测试。
- 使用关键字 and 和 or 的测试。
- 测试特定的值是否包含在列表中。
- 测试特定的值是否未包含在列表中。
# - 检查两个字符串相等和不等。
print("-检查两个字符串相等和不等。")
char_1 = 'hello'
char_2 = 'hi'
print(char_1 == char_2)
char_2 = 'hello'
print(char_1 == char_2)
# - 使用方法lower() 的测试。
print("\n-使用方法 lower() 的测试。")
char_2 = 'Hell'
print(char_1.lower() == char_2.lower())
char_2 = 'Hello'
print(char_1.lower() == char_2.lower())
# - 涉及相等、不等、大于、小于、大于等于和小于等于的数值测试。
print("\n-涉及相等、不等、大于、小于、大于等于和小于等于的数值测试。")
age_1 = 18
age_2 = 22
print(age_1 == age_2)
age_2 = 18
print(age_1 == age_2)
age_2 = 22
print(age_1 != age_2)
age_1 = 22
print(age_1 != age_2)
print(age_1 > age_2)
age_2 = 18
print(age_1 > age_2)
print(age_1 < age_2)
age_1 = 17
print(age_1 < age_2)
print(age_1 >= age_2)
age_1 = 22
print(age_1 >= age_2)
print(age_1 <= age_2)
age_2 = 25
print(age_1 <= age_2)
# - 使用关键字 and 和 or 的测试。
print("\n-使用关键字 and 和 or 的测试。")
print(age_1 > 22 and age_2 > 22)
age_1 = 23
print(age_1 > 22 and age_2 > 22)
# - 测试特定的值是否包含在列表中。
print("\n-测试特定的值是否包含在列表中。")
days = ['sunday','monday','tuesday']
print('sunday' in days)
print('wednesday' in days)
# - 测试特定的值是否未包含在列表中。
print("\n-测试特定的值是否未包含在列表中。")
print('sunday' not in days)
print('wednesday' not in days)
输出:
-检查两个字符串相等和不等。
False
True
-使用方法 lower() 的测试。
False
True
-涉及相等、不等、大于、小于、大于等于和小于等于的数值测试。
False
True
True
False
False
True
False
True
False
True
False
True
-使用关键字 and 和 or 的测试。
False
True
-测试特定的值是否包含在列表中。
True
False
-测试特定的值是否未包含在列表中。
False
True
练习5-3
外星人颜色 假设在游戏中刚射杀了一个外星人,请创建一个名为alien_color 的变量,并将其赋值为’green’、‘yellow’ 或’red’ 。
- 编写一条if 语句,检查外星人是否是绿色的。如果是,就打印一条消息,指出玩家获得了5分。
- 编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本中未通过(未通过测试时没有输出)。
alien_color = 'green'
if alien_color == 'green':
print("You just earned 5 points!")
alien_color = 'red'
if alien_color == 'green':
print("You just earned 5 points!")
输出:
You just earned 5 points!
练习5-4
外星人颜色2 像练习5-3那样设置外星人的颜色,并编写一个if-else 结构。
- 如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了5分。
- 如果外星人不是绿色的,就打印一条消息,指出玩家获得了10分。
- 编写这个程序的两个版本,在一个版本中执行if 代码块,在另一个版本中执行else 代码块。
alien_color = 'green'
if alien_color == 'green':
print("You just earned 5 points!")
else:
print("You just earned 10 points!")
alien_color = 'yellow'
if alien_color == 'green':
print("You just earned 5 points!")
else:
print("You just earned 10 points!")
输出:
You just earned 5 points!
You just earned 10 points!
练习5-5
外星人颜色3 将练习5-4中的if-else 结构改为if-elif-else 结构。
- 如果外星人是绿色的,就打印一条消息,指出玩家获得了5分。
- 如果外星人是黄色的,就打印一条消息,指出玩家获得了10分。
- 如果外星人是红色的,就打印一条消息,指出玩家获得了15分。
- 编写这个程序的三个版本,分别在外星人为绿色、黄色和红色时打印一条消息。
alien_color = 'green'
if alien_color == 'green':
print("You just earned 5 points!")
elif alien_color == 'yellow':
print("You just earned 10 points!")
elif alien_color == 'red':
print("You just earned 15 points!")
alien_color = 'yellow'
if alien_color == 'green':
print("You just earned 5 points!")
elif alien_color == 'yellow':
print("You just earned 10 points!")
elif alien_color == 'red':
print("You just earned 15 points!")
alien_color = 'red'
if alien_color == 'green':
print("You just earned 5 points!")
elif alien_color == 'yellow':
print("You just earned 10 points!")
elif alien_color == 'red':
print("You just earned 15 points!")
输出:
You just earned 5 points!
You just earned 10 points!
You just earned 15 points!
练习5-6
人生的不同阶段 设置变量 age 的值,再编写一个 if-elif-else 结构,根据 age 的值判断一个人处于人生的哪个阶段。
- 如果年龄小于2岁,就打印一条消息,指出这个人是婴儿。
- 如果年龄为2(含)~4岁,就打印一条消息,指出这个人是幼儿。
- 如果年龄为4(含)~13岁,就打印一条消息,指出这个人是儿童。
- 如果年龄为13(含)~20岁,就打印一条消息,指出这个人是青少年。
- 如果年龄为20(含)~65岁,就打印一条消息,指出这个人是成年人。
- 如果年龄超过65岁(含),就打印一条消息,指出这个人是老年人。
age = 22
if age < 2:
print("You're a baby!")
elif age <4:
print("You're a toddler!")
elif age < 13:
print("You're a kid!")
elif age < 20:
print("You're a teenager!")
elif age < 65:
print("You're a adult!")
else:
print("You're a elder!")
输出:
You're a adult!
练习5-7
喜欢的水果 创建一个列表,其中包含你喜欢的水果,再编写一系列独立的if 语句,检查列表中是否包含特定的水果。
- 将该列表命名为favorite_fruits ,并在其中包含三种水果。
- 编写5条if 语句,每条都检查某种水果是否包含在列表中。如果是,就打印一条消息,下面是一个例子。
You really like bananas!
favorite_fruits = ['watermelon', 'strawberry', 'banana']
if 'watermelon' in favorite_fruits:
print("You really like watermelon!")
if 'strawberry' in favorite_fruits:
print("You really like strawberry!")
if 'peach' in favorite_fruits:
print("You really like peach!")
if 'banana' in favorite_fruits:
print("You really like banana!")
if 'grape' in favorite_fruits:
print("You really like grape!")
输出:
You really like watermelon!
You really like strawberry!
You really like banana!
练习5-8
以特殊方式跟管理员打招呼 创建一个至少包含5个用户名的列表,且其中一个用户名为’admin’ 。想象你要编写代码,在每位用户登录网站后都打印一条问候消息。遍历用户名列表,并向每位用户打印一条问候消息。
- 如果用户名为’admin’ ,就打印一条特殊的问候消息,如下所示。
Hello admin, would you like to see a status report? - 否则,打印一条普通的问候消息,如下所示。
Hello Jaden, thank you for logging in again
usernames = ['eric', 'willie', 'admin', 'erin', 'ever']
for username in usernames:
if username == 'admin':
print("Hello admin, would you like to see a status report?")
else:
print(f"Hello {username}, thank you for loggin in again!")
输出:
Hello eric, thank you for loggin in again!
Hello willie, thank you for loggin in again!
Hello admin, would you like to see a status report?
Hello erin, thank you for loggin in again!
Hello ever, thank you for loggin in again!
练习5-9
处理没有用户的情形 在为完成练习5-8编写的程序中,添加一条if 语句,检查用户名列表是否为空。
- 如果为空,就打印如下消息。
We need to find some users! - 删除列表中的所有用户名,确定将打印正确的消息。
usernames = []
if usernames:
for username in usernames:
if username == 'admin':
print("Hello admin, would you like to see a status report?")
else:
print(f"Hello {username}, thank you for loggin in again!")
else:
print("We need to find some users!")
输出:
We need to find some users!
练习5-10
检查用户名 按下面的说明编写一个程序,模拟网站如何确保每位用户的用户名都独一无二。
- 创建一个至少包含5个用户名的列表,并将其命名为 current_users 。
- 再创建一个包含5个用户名的列表,将其命名为 new_users ,并确保其中有一两个用户名也包含在列表 current_users 中。
- 遍历列表 new_users ,对于其中的每个用户名,都检查它是否已被使用。如果是,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指出这个用户名未被使用。
- 确保比较时不区分大小写。换句话说,如果用户名’John’ 已被使用,应拒绝用户名’JOHN’ 。(为此,需要创建列表 current_users 的副本,其中包含当前所有用户名的小写版本。)
current_users = ['eric', 'willie', 'admin', 'erin', 'ever']
new_users = ['sarah', 'Willie', 'PHIL', 'ever', 'Iona']
for new_user in new_users:
if new_user.lower() in current_users:
print(f"Sorry {new_user}, that name is taken.")
else:
print(f"Great, {new_user} is still available.")
输出:
Great, sarah is still available.
Sorry Willie, that name is taken.
Great, PHIL is still available.
Sorry ever, that name is taken.
Great, Iona is still available.
练习5-11
序数 序数表示位置,如1st和2nd。序数大多以th结尾,只有1、2和3例外。
- 在一个列表中存储数字1~9。
- 遍历这个列表。
- 在循环中使用一个if-elif-else 结构,以打印每个数字对应的序数。输出内容应为"1st 2nd 3rd 4th 5th 6th 7th 8th 9th" ,但每个序数都独占一行。
numbers = list(range(1, 10))
for number in numbers:
if number == 1:
print(f"{number}st")
elif number == 2:
print(f"{number}nd")
elif number == 3:
print(f"{number}rd")
else:
print(f"{number}th")
输出:
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
练习5-12
设置if 语句的格式 审核你在本章编写的程序,确保正确地设置了条件测试的格式。
练习5-13
自己的想法 与刚拿起本书时相比,现在你是一名能力更强的程序员了。鉴于你对如何在程序中模拟现实情形有了更深入的认识,可以考虑使用程序来解决一些问题了。随着编程技能不断提高,你可能想解决一些问题,请将这方面的
想法记录下来。想想你可能想编写的游戏、想研究的数据集以及想创建的Web应用程序。