Pythonを独学して入門から放棄まで、12

15283 ワード

練習する


1.少なくとも5人のユーザー名を含むリストを作成し、そのうちの1人のユーザー名は「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?
'
) else: print('Hello',Name_List,', thank you for logging in again!
'
) 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.ユーザー名のチェック:以下の説明に従ってプログラムを作成し、Webサイトをシミュレートして、各ユーザーのユーザー名がユニークであることを確認します.3.1少なくとも5人のユーザー名を含むリストを作成し、current_と名前を付けます.users.
current_users = ['zhangsan','lisi','wangwu','zhaoliu','licaicai']

3.2さらに5人のユーザー名を含むリストを作成し、new_と名前を付けます.usersを使用して、リストcurrent_に1つまたは2つのユーザー名が含まれていることを確認します.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.
'
) else: print(new_user,'This user name is not used.
'
) 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.序数:序数は位置を表し、例えば1 stと2 ndである.ほとんどのシーケンスは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構造を使用して、各数字に対応するシーケンス数を印刷します.出力内容は1 st,2 nd,3 rd,4 th,5 th,6 th,7 th,8 th,9 thであるべきであるが,各シーケンスは1行を独占する.
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
>>>