Python入門から実践まで5-8課後練習問題

6708 ワード

5.8
管理者に特別な方法で挨拶:少なくとも5つのユーザー名を含むリストを作成し、そのうちの1つを作成します.
個のユーザー名は「admin」です.コードを作成し、ユーザー一人一人がウェブサイトにログインした後、挨拶メッセージを印刷することを想像してください.ユーザー名のリストを巡り、各ユーザーに挨拶メッセージを印刷します.ユーザー名が「admin」の場合、「Hello admin,would you like to see a status report?」などの特別な挨拶メッセージが印刷されます.そうでなければ、「Hello Eric,thank you for logging in again」などの一般的な挨拶メッセージを印刷します.
names = ['admin', 'David', 'Jone', 'Mickle', 'Jane']
for name in names:
    if 'admin' in name:
        print("Hello admin, would you like to see a status report?")
    else:
        print("Hello Eric,thank you for logging in again")
  1:else  print   4 。
  2:   else      。for        。
5.9
         :      5-8       ,    if   , 
          。
     ,     “We need to find some users!”。
            ,          。
 
  
#  if               
names = ['David', 'admin', 'Jone', 'Mickle', 'Jane']
if names:
    for name in names:
        if 'admin' in name:
            print("Hello admin, would you like to see a status report?")
        else:
            print("Hello Eric,thank you for logging in again")
else:
    print("We need find some users!")
5.10     :            ,              
        。
         5        ,      current_users。
        5        ,     new_users,        
          current_users  。
     new_users,          ,          。   
  ,       ,           ;  ,      , 
          。
current_users = ['Jone', 'David', 'John', 'Franck', 'admin']
new_users = ['Lemon', 'Jone', 'Len', 'John', 'Hayley']
for new_users in current_users:
    if new_users.lower() in current_users.lower():
        print("The name has created,please change the name.^_^")
    else:
        print("OK,the name which you named could use,please enter")
  :       ,     ,        ,          ,     !
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(str(number) +'st')
    elif number == 2:
        print(str(number) + 'nd')
    elif number == 3:
        print(str(number) + 'rd')
    else:
        print(str(number) + 'th')