第三周作业
2018-03-21 20:24
525 查看
5-3 外星人颜色#1 :
假设在游戏中刚射杀了一个外星人,请创建一个名为alien_color 的变量,并将其设置为'green' 、'yellow' 或'red' 。编写一条if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出玩家获得了5个点。编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本中未通过(未通过测试时没有输出)。 alien_color = "red"
if alien_color == "green":
print("player get five points")
alien_color = "green"
if alien_color == "green":
print("player get five points")
5-5 外星人颜色#3 :
将练习5-4中的if-else 结构改为if-elif-else 结构。如果外星人是绿色的,就打印一条消息,指出玩家获得了5个点。如果外星人是黄色的,就打印一条消息,指出玩家获得了10个点。如果外星人是红色的,就打印一条消息,指出玩家获得了15个点。编写这个程序的三个版本,它们分别在外星人为绿色、黄色和红色时打印一条消息。
创建一个至少包含5个用户名的列表,且其中一个用户名为'admin' 。想象你要编写代码,在每位用户登录网站后都打印一条问候消息。遍历用户名列表,并向每位用户打印一条问候消息。
如果用户名为'admin' ,就打印一条特殊的问候消息,如“Hello admin, would you like to see a status report?”。
否则,打印一条普通的问候消息,如“Hello Eric, thank you for logging in again”。
5-9 处理没有用户的情形 :
在为完成练习5-8编写的程序中,添加一条if 语句,检查用户名列表是否为空。
如果为空,就打印消息“We need to find some users!”。删除列表中的所有用户名,确定将打印正确的消息。
5-11 序数 :
序数表示位置,如1st和2nd。大多数序数都以th结尾,只有1、2和3例外。
在一个列表中存储数字1~9。遍历这个列表。
在循环中使用一个if-elif-else 结构,以打印每个数字对应的序数。输出内容应为1st 、2nd 、3rd 、4th 、5th 、6th 、7th 、8th 和9th ,但每个序数都独占一行。
6-5 河流 :
创建一个字典,在其中存储三条大河流及其流经的国家。其中一个键—值对可能是'nile': 'egypt' 。使用循环为每条河流打印一条消息,如“The Nile runs through Egypt.”。使用循环将该字典中每条河流的名字都打印出来。
使用循环将该字典包含的每个国家的名字都打印出来。
6-8 宠物 :
创建多个字典,对于每个字典,都使用一个宠物的名称来给它命名;在每个字典中,包含宠物的类型及其主人的名字。将这些字典存储在一个名为pets的列表中,再遍历该列表,并将宠物的所有信息都打印出来。
6-9 喜欢的地方 :
创建一个名为favorite_places 的字典。在这个字典中,将三个人的名字用作键;对于其中的每个人,都存储他喜欢的1~3个地方。为让这个练习更有趣些,可让一些朋友指出他们喜欢的几个地方。遍历这个字典,并将其中每个人的名字及其喜欢的地方打印出来。
6-11 城市 :
创建一个名为cities 的字典,其中将三个城市名用作键;对于每座城市,都创建一个字典,并在其中包含该城市所属的国家、人口约数以及一个有关该
城市的事实。在表示每座城市的字典中,应包含country 、population 和fact 等键。将每座城市的名字以及有关它们的信息都打印出来。
假设在游戏中刚射杀了一个外星人,请创建一个名为alien_color 的变量,并将其设置为'green' 、'yellow' 或'red' 。编写一条if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出玩家获得了5个点。编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本中未通过(未通过测试时没有输出)。 alien_color = "red"
if alien_color == "green":
print("player get five points")
alien_color = "green"
if alien_color == "green":
print("player get five points")
5-5 外星人颜色#3 :
将练习5-4中的if-else 结构改为if-elif-else 结构。如果外星人是绿色的,就打印一条消息,指出玩家获得了5个点。如果外星人是黄色的,就打印一条消息,指出玩家获得了10个点。如果外星人是红色的,就打印一条消息,指出玩家获得了15个点。编写这个程序的三个版本,它们分别在外星人为绿色、黄色和红色时打印一条消息。
alien_color = "red" if alien_color == "green": print("player get five points") elif alien_color == "yellow": print("player get ten points") else: print("player get fifteen points")5-8 以特殊方式跟管理员打招呼 :
创建一个至少包含5个用户名的列表,且其中一个用户名为'admin' 。想象你要编写代码,在每位用户登录网站后都打印一条问候消息。遍历用户名列表,并向每位用户打印一条问候消息。
如果用户名为'admin' ,就打印一条特殊的问候消息,如“Hello admin, would you like to see a status report?”。
否则,打印一条普通的问候消息,如“Hello Eric, thank you for logging in again”。
names = ["admin","alice","eric","bob","tom"] for i in names: if i == "admin": print("Hello Admin, would you like to see a status report?") else: print("Hello "+i.title()+", thank you for logging in again")
5-9 处理没有用户的情形 :
在为完成练习5-8编写的程序中,添加一条if 语句,检查用户名列表是否为空。
如果为空,就打印消息“We need to find some users!”。删除列表中的所有用户名,确定将打印正确的消息。
names = ["admin","alice","eric","bob","tom"] if names: for i in names: if i == "admin": print("Hello Admin, would you like to see a status report?") else: print("Hello "+i.title()+", thank you for logging in again") else : print("We need to find some users!")
5-11 序数 :
序数表示位置,如1st和2nd。大多数序数都以th结尾,只有1、2和3例外。
在一个列表中存储数字1~9。遍历这个列表。
在循环中使用一个if-elif-else 结构,以打印每个数字对应的序数。输出内容应为1st 、2nd 、3rd 、4th 、5th 、6th 、7th 、8th 和9th ,但每个序数都独占一行。
nums = list(range(1,10)) for i in nums: if i == 1: print("1st") elif i == 2: print("2nd") elif i == 3: print("3rd") else: print(str(i)+"nd")
6-5 河流 :
创建一个字典,在其中存储三条大河流及其流经的国家。其中一个键—值对可能是'nile': 'egypt' 。使用循环为每条河流打印一条消息,如“The Nile runs through Egypt.”。使用循环将该字典中每条河流的名字都打印出来。
使用循环将该字典包含的每个国家的名字都打印出来。
rivers = {'nile': 'egypt','changjiang':'china','Danube':'germany'} for rivern,country in rivers.items(): print("The "+rivern.title()+" runs through "+country.title()+".")
6-8 宠物 :
创建多个字典,对于每个字典,都使用一个宠物的名称来给它命名;在每个字典中,包含宠物的类型及其主人的名字。将这些字典存储在一个名为pets的列表中,再遍历该列表,并将宠物的所有信息都打印出来。
alice = {'type':'cat','master':'bob'} tom = {'type':'dog','master':'emly'} black = {'type':'rabbit','master':'admin'} pets = [alice,tom,black] for pet in pets: for i,j in pet.items(): if i == 'type': print("pet type:" + j) else: print("master:"+j.title()) print()
6-9 喜欢的地方 :
创建一个名为favorite_places 的字典。在这个字典中,将三个人的名字用作键;对于其中的每个人,都存储他喜欢的1~3个地方。为让这个练习更有趣些,可让一些朋友指出他们喜欢的几个地方。遍历这个字典,并将其中每个人的名字及其喜欢的地方打印出来。
favorite_places = { 'admin':['thailand','america','germany'], 'bob':['shenzhen','guangzhou','shanghai'], 'tom':['india','japan','england'] } for name,places in favorite_places.items(): print(name+':') for place in places: print(place) print()
6-11 城市 :
创建一个名为cities 的字典,其中将三个城市名用作键;对于每座城市,都创建一个字典,并在其中包含该城市所属的国家、人口约数以及一个有关该
城市的事实。在表示每座城市的字典中,应包含country 、population 和fact 等键。将每座城市的名字以及有关它们的信息都打印出来。
cities = { 'Los Angeles':{ 'country':'america', 'population':3976000, 'fact':'Disneyland is in Los Angeles suburb.' }, 'Chicago':{ 'country':'america', 'population':1003000, 'fact':'Chicago has 6 graduate and professional schools of high repute.' }, 'Saint Petersburg':{ 'country':'russian', 'population':5132000, 'fact':'1703-Tsar Peter the Great founds the city of Saint Petersburg.' } } for i,j in cities.items(): print(i+':') for k,t in j.items(): print(k+':'+str(t))
相关文章推荐
- 9.20第三周java作业
- 第三周作业
- 第三周作业三
- 第三周作业——工资管理系统(练习文件操作)
- AndrewNg机器学习第三周作业:关于验证自己得出的Hypothesis的准确度
- 第三周作业
- 第三周作业
- Andrew Deeplearning.ai 第三周作业记录
- 2017-2018-1 JAVA实验站 第三周作业
- 第三周作业HAproxy文件操作
- 第三周作业——第五章动手试一试
- N26-第三周作业
- 第三周作业
- 学习总结|Linux内核分析 第三周作业 - 网易云课堂
- 第三周作业
- 算法作业_5(2017.3.7第三周)
- 第三周作业-实现随机点名的签到程序 第五需求,头像版
- 第三周作业--实现随机点名的签到程序
- 第三周作业:判断是否闰年
- 软件工程专业导论第三周作业