Python 猜数字与模拟登陆

来源:互联网 发布:vm虚拟机安装linux教程 编辑:程序博客网 时间:2024/06/07 09:07

猜数字程序:

# @Author Jonathon# @Software: PyCharm Community Edition# @time:2017/10/2319:59age=56count=0while count<3:    age111 =int(input('age:'))    if age111==age:        print('you are right')        break    elif age111>age:        print('please smaller')    else:        print('please bigger')    count+=1 #如果三个if else后面都加break,count这一句就会报错    if count==3:        continue_flag=input('do you want go on?')        if continue_flag!='n':            count=0else:    print('too many times.please wait one hour')

模拟登陆(与猜数字类似)

# @Author Jonathon# @Software: PyCharm Community Edition# @time:2017/10/2321:10import osimport jsoncount=0records=[]locks=[]login_flag=Falsepath='D:/namepwd.txt'  #文本文件中的字典key,value必须是双引号,否则pycharm报错for line in open(path):    records.append(json.loads(line))while count<3:    username=input('请输入用户名:')    password=input('请输入密码:')    for item in records:        if  (item['name']==username and item['pwd']==password):            print('welcome {name}'.format(name=username))            login_flag=True            break        else:            continue    if login_flag==True:        break    count += 1    if count==3:        print('超过三次错误,已经锁定!')   #如果三次都没输入正确,锁定帐号        with open('D:/locks.txt','a') as file:  #追加写入文件            file.write('\n')            file.write(username)        break


原创粉丝点击