標籤:1.3 data- 知識點 不能 user 日期 username and str
一、用 while 迴圈、for 迴圈實現
- username = joseph,passwd = 123456
- 讓使用者輸入帳號和密碼,輸入使用者和密碼輸入正確的話,提示你 xxx,歡迎登入,今天的日期是xxx,程式結束
- 錯誤的話,提示帳號/密碼輸入錯誤,最多輸入3次,如果輸入3次都沒有登入成功,提示失敗次數過多。
- 需要判斷輸入是否為空白,輸入空也算輸入錯誤一次
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 break13 else:14 print(‘帳號/密碼輸入錯誤‘)15 else:16 print(‘帳號/密碼輸入錯誤‘)17 else:18 print(‘失敗次數過多‘)
1 # 使用 While 迴圈 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 get_username.strip() == str(username).strip():11 if get_passwd.strip() == str(passwd).strip():12 print(‘%s,歡迎登入,今天的日期是%s‘ %(get_username,today))13 break14 else:15 print(‘帳號/密碼輸入錯誤‘)16 else:17 print(‘帳號/密碼輸入錯誤‘)18 count = count + 119 else:20 print(‘失敗次數過多‘)
知識點:
- while...else,for...else 的用法
- .strip() 的用法
二、增加提示“使用者名稱/密碼不可為空”
1 import datetime 2 today = datetime.date.today() 3 username = ‘joseph‘ 4 passwd = 123456 5 count = 0 6 while count < 3: 7 get_username = input(‘Please input username:‘) 8 get_passwd = input(‘Please input passwd:‘) 9 if get_username.strip() != ‘‘ and get_passwd != ‘‘:10 if get_username.strip() == str(username).strip():11 if get_passwd == str(passwd):12 print(‘%s,歡迎登入,今天的日期是%s‘ %(get_username,today))13 break14 else:15 print(‘帳號/密碼輸入錯誤‘)16 else:17 print(‘帳號/密碼輸入錯誤‘)18 else:19 print(‘使用者名稱/密碼不可為空‘)20 count = count + 121 else:22 print(‘失敗次數過多‘)
知識點:
python 實現功能之:登入