Use an if statement processing list (python from entry to after-school exercise)

79 - 80 school exercise 

#5-8 以特殊方式跟管理员打招呼:创建一个至少包含 5 个用户名的列表,且其中一
#个用户名为'admin'。想象你要编写代码,在每位用户登录网站后都打印一条问候消息。
#遍历用户名列表,并向每位用户打印一条问候消息。
#如果用户名为'admin',就打印一条特殊的问候消息,如“Hello admin, would you like to see a status report?”。
# 否则,打印一条普通的问候消息,如“Hello Eric, thank you for logging in again”。
users = ['li','wang','liu','zhang','admin','root']
for user in users:
    if user == 'admin':
        print('Hello admin, would you like to see a status report?')
    else:
        print('Hello '+ user+', thank you for logging in again .')

#5-9 处理没有用户的情形:在为完成练习 5-8 编写的程序中,添加一条 if 语句,检
#查用户名列表是否为空。
# 如果为空,就打印消息“We need to find some users!”。
#删除列表中的所有用户名,确定将打印正确的消息。
users1 = ['li','wang','liu','zhang','admin','root']
if users1:
    print(users1)
else:
    print('We need to find some users!')

users2 = []
if users2:
    print(users2)
else:
    print('We need to find some users!')
#5-10 检查用户名:按下面的说明编写一个程序,模拟网站确保每位用户的用户名都独一无二的方式。
# 创建一个至少包含 5 个用户名的列表,并将其命名为 current_users。
#再创建一个包含 5 个用户名的列表,将其命名为 new_users,并确保其中有一两个用户名也包含在列表 current_users 中。
# 遍历列表 new_users,对于其中的每个用户名,都检查它是否已被使用。如果是
#这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指出这个用户名未被使用。
#确保比较时不区分大消息;换句话说,如果用户名'John'已被使用,应拒绝用户名'JOHN'。
current_users = ['wang','liu','zhang','admin','root']
new_users = ['Wang','admin','hao','zhanG','hadeng']
for user_name in new_users:
    if user_name in current_users or user_name.title() in current_users or user_name.lower() in current_users or user_name.upper() in current_users:
        print('用户名:'+user_name+'已被使用,请输入别的用户名!')
    else:
        print('用户名:'+user_name+'未被使用,可以使用这个用户名.')


#5-11 序数:序数表示位置,如 1st 和 2nd。大多数序数都以 th 结尾,只有 1、 2 和 3例外。
# 在一个列表中存储数字 1~9。
# 遍历这个列表。
# 在循环中使用一个 if-elif-else 结构,以打印每个数字对应的序数。输出内容
#应为 1st、 2nd、 3rd、 4th、 5th、 6th、 7th、 8th 和 9th, 但每个序数都独占一行。
array=[]
for value in range(1,10):
    if value == 1:
        array.append('1st')
    elif value == 2:
        array.append('2nd')
    elif value == 3:
        array.append('3rd')
    else:
        array.append(str(value)+'th')

print(array)

Output:

The Hello li, Thank you for logging in Again.
The Hello wang, Thank you for logging in Again.
The Hello liu, Thank you for logging in Again.
The Hello zhang, Thank you for logging in Again.
The Hello ADMIN, Would you like to See A Status ? Report
. the Hello root, Thank you for logging in Again
[ 'li', 'wang', 'liu', 'zhang', 'ADMIN', 'root']
We need to the Find some the users!
username: Wang has been use, enter another username!
Username: admin is already in use, please enter a different user name!
Username: hao not in use, you can use this username.
Username: zhanG already in use, please enter a different user name!
Username: hadeng not in use, you can use this user name.
[ '1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th' ]

Process finished with exit code 0

Published 101 original articles · won praise 73 · views 120 000 +

Guess you like

Origin blog.csdn.net/usstmiracle/article/details/103881359