Python编程从⼊门到实践:习题5~8-5-11
#5-8 以特殊⽅式跟管理员打招呼:创建⼀个⾄少包含5个⽤户名的列表,且其中⼀个⽤户名为'admin' 。
#想象你要编写代码,在每位⽤户登录⽹站后都打印⼀条问候消息。
#遍历⽤户名列表,并向每位⽤户打印⼀条问候消息。如果⽤户名为'admin' ,就打印⼀条特殊的问候消息,
#如“Hello admin, would you like to see a status report?”。
#否则,打印⼀条普通的问候消息,如“Hello Eric, thank you for logging in again”。
users = ['⽩起','周瑜','铠','太⼄真⼈','admin']
print(users)
if '⽩起' in users:
print('欢迎登陆!')
for value in users:
if value == 'admin':
print('Hello admin, would you like to see a status report?')
else:
print('Hello', value, ',thank you for logging in again')
#5-9 处理没有⽤户的情形:在为完成练习5-8编写的程序中,添加⼀条if 语句,检查⽤户名列表是否为空。
#如果为空,就打印消息“We need to find some users!”。
#删除列表中的所有⽤户名,确定将打印正确的消息。
user = []
if user:
print('欢迎登陆!')
else:
print('We need to find some users!')
#5-10 检查⽤户名:按下⾯的说明编写⼀个程序,模拟⽹站确保每位⽤户的⽤户名都独⼀⽆⼆的⽅式。
#创建⼀个⾄少包含5个⽤户名的列表,并将其命名为current_users 。
#再创建⼀个包含5个⽤户名的列表,将其命名为new_users ,
#并确保其中有⼀两个⽤户名也包含在列表current_users 中。
#遍历列表new_users ,对于其中的每个⽤户名,都检查它是否已被使⽤。
#如果是这样,就打印⼀条消息,指出需要输⼊别的⽤户名;否则,打印⼀条消息,指出这个⽤户名未被使⽤。
#确保⽐较时不区分⼤消息;换句话说,如果⽤户名'John' 已被使⽤,应拒绝⽤户名'JOHN' 。
current_users = ['⽩起','周瑜','铠','太⼄真⼈','admin']
new_users = ['⽩起','妲⼰','李⽩','铠','ADMIN']
for value in new_users:
value = value.lower()
if value in current_users:
print(value,'已被使⽤!请输⼊别的⽤户名:')
else:
print(value,'此⽤户名可以使⽤!')
#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,11))
print(numbers)
for value in numbers:
value = str(value)
if value == '1':
print(value + 'st')
elif value == '2':
print(value + 'nd')
elif value == '3':
print(value + 'rd')python编程:从入门到实践第二版
else:
print(value + 'th')

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。