自学Python从入门到放弃,12

练习

1. 创建一个至少包含 5 个用户名的列表,且其中一个用户名为’admin’。想象你要编写代码,在每位用户登录网站后都打印一条问候消息。遍历用户名列表,并向每位用户打印一条问候消息。

Name_List_s = ['zhangsan','admin','lisi','wangwu','zhaoliu']
for Name_List in Name_List_s:
  if Name_List == 'admin':
    print('Hello',Name_List,', would you like to see a status report?\n')
  else:
    print('Hello',Name_List,', thank you for logging in again!\n')

Hello zhangsan , thank you for logging in again!

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

Hello lisi , thank you for logging in again!

Hello wangwu , thank you for logging in again!

Hello zhaoliu , thank you for logging in again!

>>> 

2. 处理没有用户的情形:添加一条 if 语句,检查用户名列表是否为空。

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

Name_List_s = []
if Name_List_s:
  for Name_List in Name_List_s:
    print('There are many users here.such as',Name_List,'.')
else:
    print('We need to find some users!')
    
We need to find some users!
>>> 

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

Name_List_s = ['zhangsan','lisi','wangwu']
if Name_List_s:
  for Name_List in Name_List_s:
    print('There are many users here.such as',Name_List,'.')
else:
    print('We need to find some users!')
    
del Name_List_s[:];

print(Name_List_s)

if Name_List_s:
  for Name_List in Name_List_s:
    print('There are many users here.such as',Name_List,'.')
else:
    print('We need to find some users!')

There are many users here.such as zhangsan .
There are many users here.such as lisi .
There are many users here.such as wangwu .
[]
We need to find some users!
>>> 

3. 检查用户名:按下面的说明编写一个程序,模拟网站确保每位用户的用户名都独一无二的方式。
3.1 创建一个至少包含 5 个用户名的列表,并将其命名为 current_users。

current_users = ['zhangsan','lisi','wangwu','zhaoliu','licaicai']

3.2 再创建一个包含 5个用户名的列表,将其命名为 new_users,并确保其中有一两个用户名也包含在列表current_users 中。

new_users = ['walala','qiqiang','wangwu','gehui','liuqiang','licaicai']

3.3 遍历列表 new_users,对于其中的每个用户名,都检查它是否已被使用。如果是这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指出这个用户名未被使用。
3.4 确保比较时不区分大消息;换句话说,如果用户名’John’已被使用,应拒绝用户名’JOHN’。

current_users = ['zhangsan','lisi','wangwu','zhaoliu','licaicai']
new_users = ['walala','qiqiang','wangwu','gehui','liuqiang','licaicai']
for new_user in new_users:
  if new_user in current_users:
    print(new_user,'has been used,please enter other user name.\n')
  else:
    print(new_user,'This user name is not used.\n')

walala This user name is not used.

qiqiang This user name is not used.

wangwu has been used,please enter other user name.

gehui This user name is not used.

liuqiang This user name is not used.

licaicai has been used,please enter other user name.

>>> 

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

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

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

4.2 遍历这个列表。

for n in number:
  print(n)

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

number = range(1,10)
for n in number:
  print(n)
  if n == 1:
    print('1st')
  elif n == 2:
    print('2nd')
  elif n == 3:
    print('3rd')
  else:
    print('%dth'%n)
1
1st
2
2nd
3
3rd
4
4th
5
5th
6
6th
7
7th
8
8th
9
9th
>>> 

你可能感兴趣的:(笔记)