第五章python作业
2018-03-23 20:28
429 查看
5-3 外星人颜色#1 :假设在游戏中刚射杀了一个外星人,请创建一个名为alien_color 的变量,并将其设置为'green' 、'yellow' 或'red' 。编写一条if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出玩家获得了5个点。编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本中未通过(未通过测试时没有输出)。alien_color='green'
if alien_color == 'green':
print("You get 5 points")
"""
output:
You get 5 points
"""
如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了5个点。
如果外星人不是绿色的,就打印一条消息,指出玩家获得了10个点。
编写这个程序的两个版本,在一个版本中执行if 代码块,而在另一个版本中执行else 代码块。alien_color='green'
if alien_color == 'green':
print("You kill a alien.You get 5 points")
else:
print("You get 10 points")
"""
output:
You kill a alien.You get 5 points
"""
如果外星人是绿色的,就打印一条消息,指出玩家获得了5个点。
如果外星人是黄色的,就打印一条消息,指出玩家获得了10个点。
如果外星人是红色的,就打印一条消息,指出玩家获得了15个点。
编写这个程序的三个版本,它们分别在外星人为绿色、黄色和红色时打印一条消息。alien_color='red'
if alien_color == 'green':
print("You kill a green alien.You get 5 points")
elif alien_color == 'yellow':
print("You kill a yellow alien.You get 10 points")
else:
print("You kill a red alien.You get 15 points")
"""
output:
You kill a red alien.You get 15 points
"""5-6 人生的不同阶段 :设置变量age 的值,再编写一个if-elif-else 结构,根据age 的值判断处于人生的哪个阶段。
如果一个人的年龄小于2岁,就打印一条消息,指出他是婴儿。
如果一个人的年龄为2(含)~4岁,就打印一条消息,指出他正蹒跚学步。
如果一个人的年龄为4(含)~13岁,就打印一条消息,指出他是儿童。
如果一个人的年龄为13(含)~20岁,就打印一条消息,指出他是青少年。
如果一个人的年龄为20(含)~65岁,就打印一条消息,指出他是成年人。
如果一个人的年龄超过65(含)岁,就打印一条消息,指出他是老年人。age=19
if age < 2:
print("He is a baby")
elif age < 4:
print("He is a toddler")
elif age < 13:
print("He is a child")
elif age < 20:
print("He is a teenager")
elif age < 65:
print("He is an adult")
else:
print("He is an old man")
"""
output:
He is a teenager
"""5-8 以特殊方式跟管理员打招呼 :创建一个至少包含5个用户名的列表,且其中一个用户名为'admin' 。想象你要编写代码,在每位用户登录网站后都打印一条问
候消息。遍历用户名列表,并向每位用户打印一条问候消息。
如果用户名为'admin' ,就打印一条特殊的问候消息,如“Hello admin, would you like to see a status report?”。
否则,打印一条普通的问候消息,如“Hello Eric, thank you for logging in again”。name_list=["Jenny","Eric","admin","Kobe","Alice"]
for name in name_list:
if name == 'admin':
print("Hello admin,would you like to see a status report?")
else:
print("Hello "+name+", thank you for logging in again")
"""
output:
Hello Jenny, thank you for logging in again
Hello Eric, thank you for logging in again
Hello admin,would you like to see a status report?
Hello Kobe, thank you for logging in again
Hello Alice, thank you for logging in again
"""5-9 处理没有用户的情形 :在为完成练习5-8编写的程序中,添加一条if 语句,检查用户名列表是否为空。
如果为空,就打印消息“We need to find some users!”。
删除列表中的所有用户名,确定将打印正确的消息。
5-10 检查用户名 :按下面的说明编写一个程序,模拟网站确保每位用户的用户名都独一无二的方式。
创建一个至少包含5个用户名的列表,并将其命名为current_users 。
再创建一个包含5个用户名的列表,将其命名为new_users ,并确保其中有一两个用户名也包含在列表current_users 中。
遍历列表new_users ,对于其中的每个用户名,都检查它是否已被使用。如果是这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指
出这个用户名未被使用。
确保比较时不区分大消息;换句话说,如果用户名'John' 已被使用,应拒绝用户名'JOHN' 。current_users=['John','Bob','Alice','Jane','James']
new_users=['james','JOHN','tony','jimmy','cindy']
for name in new_users:
check=False
for name1 in current_users:
if name.upper() == name1.upper():
print('This name has been used, You need to input a new name')
check=True
break
if check == False:
print("This name is new")
"""
output:
This name has been used, You need to input a new name
This name has been used, You need to input a new name
This name is new
This name is new
This name is new
"""5-11 序数 :序数表示位置,如1st和2nd。大多数序数都以th结尾,只有1、2和3例外。
在一个列表中存储数字1~9。
遍历这个列表。
在循环中使用一个if-elif-else 结构,以打印每个数字对应的序数。输出内容应为1st 、2nd 、3rd 、4th 、5th 、6th 、7th 、8th 和9th ,但每个序数都独占一行。num_list=list(range(1,10));
for num in num_list:
if num == 1:
print(str(num)+"st")
elif num == 2:
print(str(num)+'nd')
elif num ==3:
print(str(num)+'rd')
else:
print(str(num)+'th')
"""
output:
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
"""
if alien_color == 'green':
print("You get 5 points")
"""
output:
You get 5 points
"""
alien_color='yellow' if alien_color == 'green': print("You get 5 points") """ output: """5-4 外星人颜色#2 :像练习5-3那样设置外星人的颜色,并编写一个if-else 结构。
如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了5个点。
如果外星人不是绿色的,就打印一条消息,指出玩家获得了10个点。
编写这个程序的两个版本,在一个版本中执行if 代码块,而在另一个版本中执行else 代码块。alien_color='green'
if alien_color == 'green':
print("You kill a alien.You get 5 points")
else:
print("You get 10 points")
"""
output:
You kill a alien.You get 5 points
"""
alien_color='red' if alien_color == 'green': print("You kill a alien.You get 5 points") else: print("You get 10 points") """ output: You get 10 points """5-5 外星人颜色#3 :将练习5-4中的if-else 结构改为if-elif-else 结构。
如果外星人是绿色的,就打印一条消息,指出玩家获得了5个点。
如果外星人是黄色的,就打印一条消息,指出玩家获得了10个点。
如果外星人是红色的,就打印一条消息,指出玩家获得了15个点。
编写这个程序的三个版本,它们分别在外星人为绿色、黄色和红色时打印一条消息。alien_color='red'
if alien_color == 'green':
print("You kill a green alien.You get 5 points")
elif alien_color == 'yellow':
print("You kill a yellow alien.You get 10 points")
else:
print("You kill a red alien.You get 15 points")
"""
output:
You kill a red alien.You get 15 points
"""5-6 人生的不同阶段 :设置变量age 的值,再编写一个if-elif-else 结构,根据age 的值判断处于人生的哪个阶段。
如果一个人的年龄小于2岁,就打印一条消息,指出他是婴儿。
如果一个人的年龄为2(含)~4岁,就打印一条消息,指出他正蹒跚学步。
如果一个人的年龄为4(含)~13岁,就打印一条消息,指出他是儿童。
如果一个人的年龄为13(含)~20岁,就打印一条消息,指出他是青少年。
如果一个人的年龄为20(含)~65岁,就打印一条消息,指出他是成年人。
如果一个人的年龄超过65(含)岁,就打印一条消息,指出他是老年人。age=19
if age < 2:
print("He is a baby")
elif age < 4:
print("He is a toddler")
elif age < 13:
print("He is a child")
elif age < 20:
print("He is a teenager")
elif age < 65:
print("He is an adult")
else:
print("He is an old man")
"""
output:
He is a teenager
"""5-8 以特殊方式跟管理员打招呼 :创建一个至少包含5个用户名的列表,且其中一个用户名为'admin' 。想象你要编写代码,在每位用户登录网站后都打印一条问
候消息。遍历用户名列表,并向每位用户打印一条问候消息。
如果用户名为'admin' ,就打印一条特殊的问候消息,如“Hello admin, would you like to see a status report?”。
否则,打印一条普通的问候消息,如“Hello Eric, thank you for logging in again”。name_list=["Jenny","Eric","admin","Kobe","Alice"]
for name in name_list:
if name == 'admin':
print("Hello admin,would you like to see a status report?")
else:
print("Hello "+name+", thank you for logging in again")
"""
output:
Hello Jenny, thank you for logging in again
Hello Eric, thank you for logging in again
Hello admin,would you like to see a status report?
Hello Kobe, thank you for logging in again
Hello Alice, thank you for logging in again
"""5-9 处理没有用户的情形 :在为完成练习5-8编写的程序中,添加一条if 语句,检查用户名列表是否为空。
如果为空,就打印消息“We need to find some users!”。
删除列表中的所有用户名,确定将打印正确的消息。
name_list=["Jenny","Eric","admin","Kobe","Alice"] if name_list: name_list.clear() print("Delete all users' name") else: print("We need to find some users!") """ output: Delete all users' name """
name_list=[] if name_list: name_list.clear() print("Delete all users' name") else: print("We need to find some users!") """ output: We need to find some users! """
5-10 检查用户名 :按下面的说明编写一个程序,模拟网站确保每位用户的用户名都独一无二的方式。
创建一个至少包含5个用户名的列表,并将其命名为current_users 。
再创建一个包含5个用户名的列表,将其命名为new_users ,并确保其中有一两个用户名也包含在列表current_users 中。
遍历列表new_users ,对于其中的每个用户名,都检查它是否已被使用。如果是这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指
出这个用户名未被使用。
确保比较时不区分大消息;换句话说,如果用户名'John' 已被使用,应拒绝用户名'JOHN' 。current_users=['John','Bob','Alice','Jane','James']
new_users=['james','JOHN','tony','jimmy','cindy']
for name in new_users:
check=False
for name1 in current_users:
if name.upper() == name1.upper():
print('This name has been used, You need to input a new name')
check=True
break
if check == False:
print("This name is new")
"""
output:
This name has been used, You need to input a new name
This name has been used, You need to input a new name
This name is new
This name is new
This name is new
"""5-11 序数 :序数表示位置,如1st和2nd。大多数序数都以th结尾,只有1、2和3例外。
在一个列表中存储数字1~9。
遍历这个列表。
在循环中使用一个if-elif-else 结构,以打印每个数字对应的序数。输出内容应为1st 、2nd 、3rd 、4th 、5th 、6th 、7th 、8th 和9th ,但每个序数都独占一行。num_list=list(range(1,10));
for num in num_list:
if num == 1:
print(str(num)+"st")
elif num == 2:
print(str(num)+'nd')
elif num ==3:
print(str(num)+'rd')
else:
print(str(num)+'th')
"""
output:
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
"""
相关文章推荐
- 高级编程 python 第五章作业
- python从入门到实践第五章的练习题作业
- Python第五章课后作业
- Python使用multiprocessing实现一个最简单的分布式作业调度系统
- python第三周作业——chapter 5:if语句,chapter 6:字典
- Python第一周编程作业
- 《Head First Python》笔记 第五章 Comprehending Data
- python3 概率论作业2
- python作业 生成随机数
- 实验与作业(Python)-文件操作
- 马哥2016全新Linux+Python高端运维班第三周作业
- 马哥2016全新Linux+Python高端运维班第七周作业
- 数据库作业第五章
- python基础之员工信息表作业
- python_01作业(编写登陆接口)
- 课程作业——Python基础之使用turtle库画出红旗
- python作业(3)
- 编程小白的第一本 python 入门书 学习笔记04 第五章 循环与判断
- 深入理解计算机系统家庭作业第五章
- Python实战计划学习作业2-2