登陸的循環、判斷

1、用 while 循環、for 循環實現ide

  1. username = joseph,passwd = 123456
  2. 讓用戶輸入帳號和密碼,輸入用戶和密碼輸入正確的話,提示你  xxx,歡迎登陸,今天的日期是xxx,程序結束
  3. 錯誤的話,提示帳號/密碼輸入錯誤,最多輸入3次,若是輸入3次都沒有登陸成功,提示失敗次數過多。
  4. 須要判斷輸入是否爲空,輸入空也算輸入錯誤一次
1 # 使用 for 循環
 2 import datetime 3 today = datetime.date.today() 4 username = 'joseph'
 5 passwd = 123456
 6 for i in range(3): 7     get_username = input('Please input username:') 8     get_passwd = input('Please input passwd:') 9     if get_username.strip() == str(username).strip(): 10         if get_passwd.strip() == str(passwd).strip(): 11             print('%s,歡迎登陸,今天的日期是%s' %(get_username,today)) 12             break
13         else: 14             print('帳號/密碼輸入錯誤') 15     else: 16         print('帳號/密碼輸入錯誤') 17 else: 18     print('失敗次數過多')
View Code
 1  1 # 使用 While 循環
 2  2 import datetime  3  3 today = datetime.date.today()  4  4 username = 'joseph'
 5  5 passwd = 123456
 6  6 count = 0  7  7 while count < 3:  8  8     get_username = input('Please input username:')  9  9     get_passwd = input('Please input passwd:') 10 10     if get_username.strip() == str(username).strip(): 11 11         if get_passwd.strip() == str(passwd).strip(): 12 12             print('%s,歡迎登陸,今天的日期是%s' %(get_username,today)) 13 13             break
14 14         else: 15 15             print('帳號/密碼輸入錯誤') 16 16     else: 17 17         print('帳號/密碼輸入錯誤') 18 18     count = count + 1
19 19 else: 20 20     print('失敗次數過多')
View Code

 知識點:spa

  • while...else,for...else 的用法
  • .strip() 的用法

2、增長提示「用戶名/密碼不能爲空」code

 1  1 import datetime  2  2 today = datetime.date.today()  3  3 username = 'joseph'
 4  4 passwd = 123456
 5  5 count = 0  6  6 while count < 3:  7  7     get_username = input('Please input username:')  8  8     get_passwd = input('Please input passwd:')  9  9     if get_username.strip() != '' and get_passwd != '': 10 10         if get_username.strip() == str(username).strip(): 11 11             if get_passwd == str(passwd): 12 12                 print('%s,歡迎登陸,今天的日期是%s' %(get_username,today)) 13 13                 break
14 14             else: 15 15                 print('帳號/密碼輸入錯誤') 16 16         else: 17 17             print('帳號/密碼輸入錯誤') 18 18     else: 19 19         print('用戶名/密碼不能爲空') 20 20     count = count + 1
21 21 else: 22 22     print('失敗次數過多')
View Code

 知識點:blog

  • 判斷字符串爲空:s.strip() == ''
 1 # 增長提示用戶名、密碼爲空
 2 import datetime  3 today = datetime.date.today()  4 username = 'joseph'
 5 passwd = 123456
 6 count = 0  7 while count < 3:  8     get_username = input('Please input username:')  9     get_passwd = input('Please input passwd:') 10     if not get_username and not get_passwd: 11         print('用戶名/密碼不能爲空') 12     elif get_username.strip() == str(username).strip(): 13         if get_passwd == str(passwd): 14             print('%s,歡迎登陸,今天的日期是%s' %(get_username,today)) 15             break
16         else: 17             print('帳號/密碼輸入錯誤') 18     else: 19         print('帳號/密碼輸入錯誤') 20     count = count + 1
21 else: 22     print('失敗次數過多')
View Code

 知識點:ip

  • 非空即真 ,非0即真。
相關文章
相關標籤/搜索