第二章后阶段作业

第一章 登录验证功能:回顾:

基础需求:

  • 让用户输入用户名密码
  • 认证成功后显示欢迎信息
  • 输错三次后退出程序

老师做的:

dic={
    'egon1':{'password':'123','count':0},
    'egon2':{'password':'123','count':0},
    'egon3':{'password':'123','count':0},

}


while True:
    name=input('username>>: ')

    if not name in dic:
        print('用户不存在')
        continue
    if dic[name]['count'] > 2:
        print('尝试次数过多,退出')
        break

    password=input('password>>: ')  #思路:此处是输入用户名比对存在后,才能输入密码。


    if password == dic[name]['password']:
        print('登录成功')
        break
    else:
        print('密码错误')
        dic[name]['count']+=1

自己加以修改:需求密码错了,只是重输密码和记录密码错误次数
while True:
name=input('username>>: ')

if not name in dic:
print('用户不存在')
continue

while 1:
password=input('password>>: ') #思路:此处是输入用户名比对存在后,才能输入密码。


if password == dic[name]['password']:
print('登录成功')
break
else:
print('密码错误')
dic[name]['count']+=1
if dic[name]['count'] >= 2:
print('密码尝试次数过多')
break #退出while 1循环。
continue #while 放在if或者else下 continue实现输错重输。

    if dic[name]['count'] >= 2:   #一次循环的结尾用大于等于2条件,实现错3次就退出。
      break#此处退出整个循环
 

升级需求:

  • 可以支持多个用户登录 (提示,通过列表(此处有误!)存多个账户信息)
  • 用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)
思考:文本文件中read()函数读出的数据只是按行计数的字符串,write()函数存入的数据类型也只是按一行或者多行来的字符串(可以将其他数据类型转为字符串,例如:‘{1:2}’)。
老师做的:
#黑名单db.txt的内容形式是:egon1|egon2| dic={ 'egon1':{'password':'123','count':0}, 'egon2':{'password':'123','count':0}, 'egon3':{'password':'123','count':0}, } while True: name=input('u>>: ') if name not in dic: print('用户不存在') continue with open('db.txt','r') as f: lock_users=f.read().split('|') if name in lock_users: print('用户%s已经被锁定' %name) break if dic[name]['count'] > 2: print('尝试次数过多,锁定') with open('db.txt','a') as f: f.write('%s|' %name) break password=input('p>>: ') if password == dic[name]['password']: print('登录成功') break else: print('用户名或密码错误') dic[name]['count']+=1


自己做的:
dic={
    'egon1':{'password':'123','count':0},
    'egon2':{'password':'123','count':0},
    'egon3':{'password':'123','count':0},

}

自己加以修改:需求密码错了,只是重输密码和记录密码错误次数
while True:
name=input('username>>: ')

if not name in dic:
print('用户不存在')
continue
   if not dic[name][a]:
     break


while 1:
password=input('password>>: ') #思路:此处是输入用户名比对存在后,才能输入密码。


if password == dic[name]['password']:
print('登录成功')
break
else:
print('密码错误')
dic[name]['count']+=1
if dic[name]['count'] > 2:
print('密码尝试次数过多')
break #退出while 1循环。
continue #while 放在if或者else下 continue实现输错重输。

    if dic[name]['count'] > 2:
      print('密码尝试次数过多,锁定账户)
      a=Fale
      break#此处退出整个循环

 

第二章 1购物车功能:回顾

主要是商品信息添加。

 #简单购物车,要求如下:

实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数加入购物列表,如果输入为空或其他非法输入则要求用户重新输入  

msg_dic={
'apple':10,
'tesla':100000,
'mac':3000,
'lenovo':30000,
'chicken':10,
} 

老师做的:
msg_dic={
'apple':10,
'tesla':100000,
'mac':3000,
'lenovo':30000,
'chicken':10,
}
goods_l=[]
while True:
    for key,item in msg_dic.items():
        print('name:{name} price:{price}'.format(price=item,name=key))
    choice=input('商品>>: ').strip()
    if not choice or choice not in msg_dic:continue
    count=input('购买个数>>: ').strip()
    if not count.isdigit():continue
    goods_l.append((choice,msg_dic[choice],count))

    print(goods_l)
 

 

2根据数据库登录验证:回顾

题目:用户登录认证功能  核心思路:文件的读

老师做的:
# inp_name=input('请输入你的名字: ').strip()
# inp_pwd=input('请输入你的密码: ').strip()
# with open(r'db.txt',mode='rt',encoding='utf-8') as f:
# for line in f:
# # 把用户输入的名字与密码与读出内容做比对
# u,p=line.strip('\n').split(':')
# if inp_name == u and inp_pwd == p:
# print('登录成功')
# break
# else:
# print('账号名或者密码错误')

自己做的:
# name=input('请输入名字:')
# pwd=input('请输入密码:')#现实情况同时得到全部信息?
    此处思路是先得到名字,然后得到密码,信息一起交给服务员比对。此处如果无限循环,判断信息不存在,就是重复输入两个信息。
    第一题那种思路是得到一个信息后就和数据库比对,不存在就实现重复输入一个信息,存在才能进行下一步操作。

# with open(r'123.txt',mode='r',encoding='utf-8') as f:
# for line in f:
# t=line.strip().split(':')
# #print(t)
# if t[0] == name :
# if pwd==t[1]:
# print('登录成功')
# break
# else:
# print('密码错误')
# break
# # else:
# # pass
# else:
# print('用户不存在')

# 老师的思路:一个用户对一个服务员。用户输入名字、密码给服务员,服务员打开记录簿,一行一行比对,
# 找到名字,同时密码也对了后,告诉用户登陆成功,就不往下找了;
# 名字对密码不对告诉用户密码错了;
# 找了一圈,找不到名字告诉用户名字不存在。

# name=input('请输入名字:')
# pwd=input('请输入密码:')#如果情况同时输入
# with open(r'123.txt',mode='r',encoding='utf-8') as f:
# for line in f:
# t=line.strip().split(':')
# #print(t)
# if t[0] == name and pwd==t[1]:
# print('登录成功')
# break
# if t[0] == name and pwd!=t[1]:
# print('密码错误')
# break#此处是表达没必要再进行下面的循环了。
# else:
# print('用户不存在')
 

 

3.根据数据库注册验证功能:回顾

题目:注册功能:  核心思路:文件的写
# name=input('username>>>: ').strip()
# pwd=input('password>>>: ').strip()
# with open('db1.txt',mode='at',encoding='utf-8') as f:
# info='%s:%s\n' %(name,pwd)
# f.write(info)

 

posted @ 2018-09-21 16:09  timm_book  阅读(36)  评论(0)    收藏  举报