PYTHON DAY1

PYTHON在linux下的安装:

Linux的yum依赖自带Python,为防止错误,此处更新其实就是再安装一个Python

查看默认Python版本
python -V
  
1、安装gcc,用于编译Python源码
    yum install gcc
2、下载源码包,https://www.python.org/ftp/python/
3、解压并进入源码文件
4、编译安装
    ./configure
    make all
    make install
5、查看版本
    /usr/local/bin/python2.7 -V
6、修改默认Python版本
    mv /usr/bin/python /usr/bin/python2.6
    ln -s /usr/local/bin/python2.7 /usr/bin/python
7、防止yum执行异常,修改yum使用的Python版本
    vi /usr/bin/yum
    将头部 #!/usr/bin/python 修改为 #!/usr/bin/python2.6
View Code

 

  通过 getpass 隐藏用户密码:

通过 getpass 隐藏用户密码,此操作只能在命令行中执行,pycharm并支持此操作
C:\Users\Administrator>python3
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import getpass
>>> password=getpass.getpass("please input your passord :")
please input your passord :                        #在此处输入密码test,由于导入了getpass模块,并不显示用户输入的密码
>>> print(password)
test
>>>
View Code

 

编写登陆接口

基础需求:

  • 让用户输入用户名密码
  • 认证成功后显示欢迎信息
  • 输错三次后退出程序
dic={
    'user1':{'password':'123','count':0},
    'user2':{'password':'123','count':0},
    'user3':{'password':'123','count':0}
}
count=0
while True:
    username = input('请输入用户名:')
    if username not in dic:
        print("用户名不存在")
        continue
    if dic[username]['count'] >=3:
        print("密码尝试次数过多,用户名已锁定,请确认用户名密码是否正确或者用其他用户登录")
        continue
    userpass = input("请输入密码 :")
    if userpass == dic[username]["password"]:
        print("登陆成功")
        break
    elif userpass !=dic[username]["password"]:
        print("密码错误,请重新输入")
        dic[username]['count'] += 1
        continue
View Code

 

升级需求:

  • 可以支持多个用户登录 (提示,通过列表存多个账户信息)
  • 用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)
dic={'user1':{'passwd':'123','count':0},
     'user2':{'passwd':'123','count':0},
     'user3':{'passwd':'123','count':0},
     'user4':{'passwd':'123','count':0}
     }
count=0
while True:
    username=input('请入用户名 :')
    if username not in dic:
        print("用户 %s 不存在" %(username))
        continue
    with open('user.txt', 'r', encoding='utf-8') as f:
        lock_user = (f.read().split('|'))
        if username in lock_user:
            print("用户 %s 已锁定" %(username))
            break
    if dic[username]['count'] >= 3:
        with open('user.txt','a') as f:
            f.write(username+'|')
            print("用户 %s 已锁定" % (username))
            break
    password=input("请输入密码 ")
    if password==dic[username]['passwd']:
        print("登陆成功 %s 欢迎回来" %username)
        break
    if password !=dic[username]['passwd']:
        print('密码错误,请重新输入')
        dic[username]['count']+=1
        continue
View Code

 

猜年龄游戏升级版 

要求:

  1. 允许用户最多尝试3次
  2. 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
  3. 如何猜对了,就直接退出
correct_answer=30
count=0
input_list=['Y','y','N','n']

while True:
    guess_age=int(input('请输入年龄: '))
    if guess_age==correct_answer:
        print("恭喜你,答对了")
        break
    else:
        count+=1
        if count > 2:
            confirm = input("请输入Y或者y继续,输入N或者n退出(输入其他值,游戏结束):")
            if confirm not in input_list:
                    break
            if confirm=='y' or confirm=='Y':
                count=0
                continue
            elif confirm=='n' or confirm=='N':
                break
View Code

 



posted @ 2017-07-07 01:22  DBKEEPER  阅读(150)  评论(0编辑  收藏  举报