python123第五周课后作业_“Python编程:从介绍到实践”第5章,第5.4节家庭作业,入门,第五章,54,课后...

本文代码是在jupyter中实现的,仅为了自我督促学习python之用。

5-8 以特殊方式跟管理员打招呼:创建一个至少包含 5个用户名的列表,且其中一个用户名为 ‘admin’ 。想象你要编写代码,在每位用户登录网站后都打印一条问候消息。遍历用户名列表,并向每位用户打印一条问候消息。

 如果用户名为 ‘admin’ ,就打印一条特殊的问候消息,如“Hello admin, would you like to see a status report?”。

 否则,打印一条普通的问候消息,如“Hello Eric, thank you for logging in again”。

代码:

names = ['Eric', 'Jeeny', 'tom', 'Jeffony', 'admin']

for name in names:

if name=='admin' in names:

print("Hello admin, would you like to see a status report ?")

else:

print("Hello " + name +','+"thank you logging in again.")

运行结果:

Hello Eric,thank you logging in again.

Hello Jeeny,thank you logging in again.

Hello tom,thank you logging in again.

Hello Jeffony,thank you logging in again.

Hello admin, would you like to see a status report ?

5-9 处理没有用户的情形:在为完成练习 5-8编写的程序中,添加一条 if 语句,检查用户名列表是否为空。

 如果为空,就打印消息“We need to find some users!”。

 删除列表中的所有用户名,确定将打印正确的消息。

代码:

names = []

if names:

for name in names:

if name=='admin' in names:

print("Hello admin, would you like to see a status report ?")

else:

print("Hello " + name +','+"thank you logging 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 = ['Eric', 'Jeeny', 'tom', 'jeffony', 'admin']

new_users = ['Susan','Bruce','Jeffony', 'Jack','admin']

for user in new_users:

if user.lower() in [current_user.lower() for current_user in current_users]:

print(user + " is already occupied, please re-enter!")

else:

print("Congratulations," + user + "can be used.")

运行结果:

Congratulations,Susancan be used.

Congratulations,Brucecan be used.

Jeffony is already occupied, please re-enter!

Congratulations,Jackcan be used.

admin is already occupied, please re-enter!

错误示范:

代码:

current_users = ['Eric', 'Jeeny', 'tom', 'jeffony', 'admin']

new_users = ['Susan','Bruce','Jeffony', 'Jack','admin']

for user in new_users:

if user.lower() in current_users.lower():

print(user + " is already occupied, please re-enter!")

else:

print("Congratulations," + user + "can be used.")

运行结果:

AttributeError Traceback (most recent call last)

in

3

4 for user in new_users:

----> 5 if user.lower() in current_users.lower():

6 print(user + " is already occupied, please re-enter!")

7 else:

AttributeError: 'list' object has no attribute 'lower'

5-11 序数:序数表示位置,如 1st和 2nd。大多数序数都以 th结尾,只有 1、2和 3例外。

 在一个列表中存储数字 1~9。

 遍历这个列表。

 在循环中使用一个 if-elif-else 结构,以打印每个数字对应的序数。输出内容应为 1st 、 2nd 、 3rd 、 4th 、 5th 、 6th 、 7th 、 8th 和 9th , 但每个序数都独占一行。

代码:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

for number in numbers:

if number==1:

print('1st')

elif number==2:

print('2nd')

elif number==3:

print('3rd')

else:

print(str(number)+'th')

运行结果:

1st

2nd

3rd

4th

5th

6th

7th

8th

9th

数字是整形的,而’th’是字符串,类型不一样,不能用“+”进行字符串连接。

解决办法是:

int转成string,函数str(number)

string转成int,函数int(string)

错误示范

代码:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

for number in numbers:

if number==1:

print('1st')

elif number==2:

print('2nd')

elif number==3:

print('3rd')

else:

print(number+'th')

运行结果:

1st

2nd

3rd

---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

in

8 print('3rd')

9 else:

---> 10 print(number+'th')

TypeError: unsupported operand type(s) for +: 'int' and 'str'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值