cars.py
cars = ['audi','bmw','subaru','toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())
toppings.py
requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
print("Hold the anchovies!")
magic_number.py
answer = 17
if answer !=42:
print("That is not the correct answer.Please try again!")
banned_users.py
banned_users = ['andrew','carolina','david']
user = 'marie'
if user not in banned_users:
print(f"{user.title()},you can post a response if you wish.")
voting.py
age= 19
if age >= 18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")
else:
print("Sorry,you are too young to vote.")
print("Please register to vote as soon as you turn 18!")
amusement_park.py
age = 12
if age < 4:
print("Your admission cost is $0.")
elif age < 18:
print("Your admission cost is $25.")
else:
print("Your admission cost is $40.")
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 40
else:
price = 20
print(f"Your admission cost is ${price}.")
toppings1.py
requested_toppings = ['mushrooms','extra cheese']
if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")
if 'pepperoni' in requested_toppings:
print("Adding pepperoni.")
if 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
print("\nFinished making your pizza!")
alien_color.py
# 练习 5-3 外星人颜色
# 假设在游戏中刚射杀了一个外星人,请创建一个名为 alien_color 的变量,
# 并将其设置为 'green' 、 'yellow' 或 'red' 。
# 编写一条 if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出玩家获得了5分。
# 编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本中未通过(未通过测试时没有输出)。
# 能够通过测试的版本:
alien_color = 'green'
if alien_color == 'green':
print("You just earned 5 points!")
# 不能够通过测试的版本:
alien_color = 'yellow'
if alien_color == 'red':
print("You just earned 5 points!")
alien_color1.py
# 练习 5-4 外星人颜色 2
# 像练习 5-3 那样设置外星人的颜色,并编写一个 if-else 结构。
# 如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了 5 分。
# 如果外星人不是绿色的,就打印一条消息,指出玩家获得了 10 分。
# 编写这个程序的两个版本,在一个版本中执行if代码块,而在另一个版本中执行else代码块。
# 执行if代码块的版本:
alien_color = 'green'
if alien_color == 'green':
print("You just earned 5 points!")
else:
print("You just earned 10 points!")
# 执行else代码块的版本:
alien_color = 'red'
if alien_color == 'green':
print("You just earned 5 points!")
else:
print("You just earned 10 points!")
alien_color2.py
# 练习 5-5 外星人颜色 3
# 将练习 5-4 中的 if-else 结构改为 if-elif-else 结构。
# 如果外星人是绿色的,就打印一条消息,指出玩家获得了 5 分。
# 如果外星人是黄色的,就打印一条消息,指出玩家获得了 10 分。
# 如果外星人是红色的,就打印一条消息,指出玩家获得了 15 分。
# 编写这个程序的三个版本,它们分别在外星人为绿色、黄色和红色时打印一条消息。
# 外星人为红色的版本:
alien_color = 'red'
if alien_color == 'green':
print("You just earned 5 points!")
elif alien_color == 'yellow':
print("You just earned 10 points!")
else:
print("You just earned 15 points!")
age_stage.py
# 练习 5-6 人生的不同阶段
# 设置变量 age 的值,再编写一个 if-elif-else 结构,根据 age 的值判断处于人生的哪个阶段:
# 如果一个人的年龄小于 2 岁,就打印一条消息,指出他是婴儿。
# 如果一个人的年龄为 2(含)~4 岁,就打印一条消息,指出他是幼儿。
# 如果一个人的年龄为 4(含)~13 岁,就打印一条消息,指出他是儿童。
# 如果一个人的年龄为 13(含)~20 岁,就打印一条消息,指出他是少年。
# 如果一个人的年龄为 20(含)~65 岁,就打印一条消息,指出他是成年人。
# 如果一个人的年龄超过 65(含)岁,就打印一条消息,指出他是老年人。
age = 34
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 an adult!")
else:
print("You're an elder!")
favorite_fruits.py
# 练习 5-7 喜欢的水果
# 创建一个列表,其中包含你喜欢的水果
# 再编写一系列独立的 if 语句,检查列表中是否包含特定的水果。
# 将该列表命名为 favorite_fruits ,并在其中包含三种水果。
# 编写 5 条 if 语句,每条都检查某种水果是否包含在列表中;
# 如果包含在列表中,就打印一条消息,如 You really like bananas!。
favorite_fruits = ['apple','banana','orange']
if 'peach' in favorite_fruits:
print("You really like peach!")
if 'pear' in favorite_fruits:
print("You really like pear!")
if 'apple' in favorite_fruits:
print("You really like apple!")
if 'orange' in favorite_fruits:
print("You really like orange!")
if 'banana' in favorite_fruits:
print("You really like banana!")
toppings2.py
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(f"Adding {requested_topping}.")
print("\nFinished making your pizza!")
requested_toppings = []
if requested_toppings:
for requested_topping in requested_toppings:
print(f"Adding {requested_topping}.")
print("\nFinished making your pizza!")
else:
print("\nAre you sure you want a plain pizza?")
print("\n")
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(f"Adding {requested_topping}.")
else:
print(f"Sorry,we don't have {requested_topping}.")
print("\nFinished making your pizza!")
usernames.py
# 练习 5-8 以特殊方式跟管理员打招呼
# 创建一个至少包含 5 个用户名的列表,且其中一个用户名为 'admin' 。
# 想象你要编写代码,在每位用户登录网站后都打印一条问候消息。
# 遍历用户名列表,并向每位用户打印一条问候消息:
# 如果用户名为 'admin' ,就打印一条特殊的问候消息,
# 如 Hello admin, would you like to see a status report?。
# 否则,打印一条普通的问候消息,如 Hello Eric, thank you for logging in again。
usernames = ['yuki','sakura','kuraki','admin','hayasi']
for username in usernames:
if username == 'admin':
print("Hello admin, would you like to see a status report?")
else:
print(f"Hello {username.title()}, thank you for logging in again。")
nusernames1.py
# 练习 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!")
usernames2.py
# 练习 5-10 检查用户名
# 按下面说的编写一个程序,模拟网站确保每位用户的用户名都独一无二的方式。
# 创建一个至少包含 5 个用户名的列表,并将其命名为 current_users 。
# 再创建一个包含 5 个用户名的列表,将其命名为 new_users ,
# 并确保其中有一两个用户名也包含在列表 current_users 中。
# 遍历列表 new_users ,对于其中的每个用户名,都检查它是否已被使用。
# 如果是这样,就打印一条消息,指出需要输入别的用户名;
# 否则,打印一条消息,指出这个用户名未被使用。
# 确保比较是不区分大小写;换句话说,如果用户名 'John' 已被使用,应拒绝用户名'JOHN'。
# 创建当前存在的5个用户列表
current_users = ['yuki','kuraki','sakura','kimu']
# 创建5个新用户列表
new_users = ['SAKURA','utada','Yuki','tanaka','kensin']
# 避免区分大小写创建current_users小写副本
current_users_lower = [user.lower() for user in current_users]
#遍历new_users列表
for new_user in new_users:
#检查是否被使用,并打印消息。
if new_user.lower() in current_users_lower:
print(f"Sorry {new_user},that name is taken.")
else:
print(f"Great! {new_user} is still available.")
ordinal_numbers.py
# 练习 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("1st")
elif number == 2:
print("2nd")
elif number == 3:
print("3rd")
else:
print(f"{number}th")