【HW】5-1至5-11的选题和代码和输出
2018-03-22 19:39
169 查看
5-8 以特殊方式跟管理员打招呼 :创建一个至少包含5个用户名的列表,且其中一个用户名为'admin' 。想象你要编写代码,在每位用户登录网站后都打印一条问候消息。遍历用户名列表,并向每位用户打印一条问候消息。 如果用户名为'admin' ,就打印一条特殊的问候消息,如“Hello admin, would you like to see a status report?”。 否则,打印一条普通的问候消息,如“Hello Eric, thank you for logging in again”。
users = [] if users : for user in users : if user == 'admin' : print('Hello admin, would you like to see a status report?') else : print('Hello '+user.title()+', thank you for logging in again') else: print('We need to find some users!')5-9 处理没有用户的情形 :在为完成练习5-8编写的程序中,添加一条if 语句,检查用户名列表是否为空。 如果为空,就打印消息“We need to find some users!”。 删除列表中的所有用户名,确定将打印正确的消息。
users = [] if users : for user in users : if user == 'admin' : print('Hello admin, would you like to see a status report?') else : print('Hello '+user.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 ,但每个序数都独占一行。
#5-11numbers = list(range(1,10))for num in numbers:if num == 1:print('1st')elif num == 2:print('2nd')elif num ==3 :print('3rd')else :print(str(num)+'th')
相关文章推荐
- 【HW-2】书本2-1至2-11中的选题,代码及输出
- 【HW】书本9-11至9-16选题代码和输出
- 【HW-3】书本3-1至3-11的选题,代码和输出
- 【HW】书本8-1至8-17选题,代码和输出
- 【HW】书本6-1至6-13选题和代码
- 【HW】4-1到4-13的选题,代码
- HDU ACM 11 2076 夹角有多大(我的代码是AC不过去的、、但我还是要放,毕竟输出都对)
- 【HW】书本7-1至7-10选题,代码和输入输出
- python输出问题,一行代码解决
- 剑指offer面试题11 数值的整数次方的Java代码实现
- Android 本地代码如何输出日志
- glade2支持C++代码的输出(1)
- 如何把rose模型产生的代码输出到指定的目录下?
- 剑指XX游戏读后感(2):网易2011笔试题详解/sizeof和strlen/交换双向链表节/dll和lib和exe/8叉树减色/11盆花组合/宠物技能遗传/看virtual继承代码写输出
- node.js 一个简单的页面输出实现代码
- 24点游戏的c++代码,能够输出所有可能的解
- iOS学习笔记11--Xcode创建一个纯代码的工程
- 2013 /3/11 省市县 三级联动 代码分享(*事先建好数据库)
- 同一段代码,不同C编译器,输出结果真的会不同啊!!!
- 为什么这段代码输出的是”Hello World”