自动化测试(二)如何用python写一个用户登陆功能

需求信息:

写一个判断登录的程序:
输入: 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

























猜你喜欢

转载自www.cnblogs.com/shiqijuemu/p/9970428.html