需求信息:
寫一個判斷登錄的程序:
輸入: username
password
最大錯誤次數是3次,輸入3次都沒有登錄成功,提示錯誤次數達到上限
需要判斷輸入是否為空,什么也不輸入,輸入一個空格、n個空格都算空
登錄成功,提示歡迎xxx,今天的日期是 xxx
可以用多個用戶登錄,選做(多個用戶登錄,每個用戶的密碼也不一樣)
知識點:
1.循環方法while和for的結合使用;2.import使用time函數;3.字典的初步使用;4.break和continue的簡單用法;
實現思路:
1.構建一個放置用戶信息的字典(鍵值對儲存用戶的賬號和密碼)
zhangmi={"wangdapang":"123456","wangerpang":"1234567","wangxiaopang":"7654321"}
2.使用for循環依次取出字典的數據
for key,value in zhangmi.items():#items()遍歷字典內容
3.使用input語句接收輸入的需要判斷的用戶名和密碼
shuru_user = input("請輸入用戶名:")
shuru_pwd = input("請輸入密碼:")
4.對輸入的值和從字典遍歷出來的數據進行比較
if shuru_user==key or shuru_pwd==value:
aaaa+=1#計數器
break#結束循環
elif shuru_pwd!=value or shuru_user!=key:
continue#用來跳出當次循環
5.外層循環控制校驗和循環校驗次數
while cccc<=3:
count += 1#校驗次數的計數器
if count>3:
print("超過3次,你沒有機會了!")
break
elif aaaa>=1:
print("歡迎" + key + "來到綠洲!現在時間"+sj)#sj是取用當前時間的變量
break
6.判斷輸入的值是否為空值
if shuru_user=="" or shuru_pwd=="":
print("賬號或密碼輸入為空!")
continue
7.引入時間戳更改輸出格式
import time,datetime
sj=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
完成
完整代碼:(比較笨的思路)
import time,datetime
sj=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
zhangmi={"wangdapang":"123456","wangerpang":"1234567","wangxiaopang":"7654321"}
count=0
cccc=0
aaaa=0
while cccc<=3:
count += 1
if count>3:
print("超過3次,你沒有機會了!")
break
elif aaaa>=1:
print("歡迎" + key + "來到綠洲!現在時間"+sj)
break
shuru_user = input("請輸入用戶名:")
shuru_pwd = input("請輸入密碼:")
if shuru_user=="" or shuru_pwd=="":
print("賬號或密碼輸入為空!")
continue
for key,value in zhangmi.items():
# if shuru_user==key:
# print("duiduidui")
if shuru_user==key or shuru_pwd==value:
aaaa+=1
break
elif shuru_pwd!=value or shuru_user!=key:
continue