更新了用户操作界面

import random
import time
import pickle
import sys
import os
import easygui as g
def encode(s):
    return ' '.join([bin(ord(c)).replace('0b', '') for c in s])             #对密码用户名进行二进制转换
 
def decode(s):
    return ''.join([chr(i) for i in [int(b, 2) for b in s.split(' ')]])
enroll =True                                                            #验证变量设定
yz =True
yzwj=False
blb=[]          
wjs =0
yzxh =True
yhzcjm = True
while yhzcjm ==True:
    g.msgbox('欢迎来到猜数字小游戏')
    dlchoise =g.choicebox('注册/登陆',choices=('注册','登陆'))
    for i in range (0,1000):        #读取本地是否有用户名,密码文件并记录
                namewj = str(i)
                passwordwj =str(i)
                nameyz=os.getcwd() + '/用户名'+namewj+'.txt'
                passwordyz=os.getcwd() + '/密码'+passwordwj+'.txt'
                if os.path.exists(nameyz):
                    wjs +=1
                else:
                    continue
    if dlchoise =='注册':
        titlezc='注册'
        namezc=g.textbox('用户名',titlezc)
        
        

        while enroll ==True:  #进入注册验证循环
                path=os.getcwd()    #读取根目录  
                for i in range (0,1000):
                        blb.append(i)   #同步遍历次数
                        if yz ==False: #同名验证
                            while yzxh ==True: 
                                for a in range (0,wjs): #遍历本地用户文件
                                    namezc =g.textbox('用户名:')
                                    blz =blb[a]
                                    namewj = str(blz)   
                                    passwordwj =str(blz)
                                    nameyz=os.getcwd() + '/用户名'+namewj+'.txt'    #构建用户存放文件路径
                                    passwordyz=os.getcwd() + '/密码'+passwordwj+'.txt'
                                    if os.path.exists(nameyz):      #检查文件是否存在
                                        namezcjm =pickle.load(open(path+'/用户名'+namewj+'.txt','rb'))      #读取文件
                                        passwordzcjm =pickle.load(open(path+'/密码'+namewj+'.txt','rb'))
                                        if namezc ==decode(namezcjm):   #判断是否同名
                                            cs =g.integerbox('此用户已经注册!按1登陆,按其他任意键重新输入用户名:')
                                            if cs==1:
                                                namezcjim=decode(namezcjm)
                                                passwordzcjim=decode(passwordzcjm)
                                                if namezc == namezcjim:
                                                    password =g.textbox('输入密码:')
                                                    if password == passwordzcjim:
                                                        g.msgbox('登陆成功!')
                                                        enroll =False
                                                        break #(搞不明白为什么break没有用,没有退出循环效果)
                                                    else :
                                                        g.msgbox('密码错误!')
                                                        sys.exit()
                                            else:
                                                yzwj=False
                                        else:
                                            yzwj=True
                                    if  yzwj ==True: #验证通过进行登记
                                        file = open(path+'/用户名'+namewj+'.txt','w')
                                        file = open(path+'/密码'+passwordwj+'.txt','w')
                                        file.close
                                        passwordzc = g.textbox('1密码:')
                                        namezcjm=encode(namezc)
                                        passwordzcjm=encode(passwordzc)
                                        pickle.dump(namezcjm,open(path+'/用户名'+namewj+'.txt','wb'))
                                        pickle.dump(passwordzcjm,open(path+'/密码'+passwordwj+'.txt','wb'))
                                        break  
                        namewj = str(i)  #正常注册
                        passwordwj =str(i)
                        nameyz=os.getcwd() + '/用户名'+namewj+'.txt'
                        passwordyz=os.getcwd() + '/密码'+passwordwj+'.txt'
                        if os.path.exists(nameyz):  
                            namezcjm =pickle.load(open(path+'/用户名'+namewj+'.txt','rb'))
                            passwordzcjm =pickle.load(open(path+'/密码'+namewj+'.txt','rb'))
                            if namezc ==decode(namezcjm):
                                cs =g.integerbox('此用户已经注册!按1登陆,按其他任意键重新输入用户名:')
                                if cs==1:
                                    namezcjim=decode(namezcjm)
                                    passwordzcjim=decode(passwordzcjm)
                                    if namezc == namezcjim:
                                        password =g.textbox('输入密码:')
                                        if password == passwordzcjim:
                                            g.msgbox('登陆成功!')
                                            enroll =False
                                            yhzcjm=False
                                            break
                                        else :
                                            g.msgbox('密码错误!')
                                            sys.exit()
                                else:  #同名验证开启
                                    enroll =False
                                    yz =False
                                    yzxh =True
                                    continue

                        else:   #z]正常注册
                            file = open(path+'/用户名'+namewj+'.txt','w')
                            file = open(path+'/密码'+passwordwj+'.txt','w')
                            file.close
                            passwordzc = g.textbox('密码:')
                            namezcjm=encode(namezc)
                            passwordzcjm=encode(passwordzc)
                            pickle.dump(namezcjm,open(path+'/用户名'+namewj+'.txt','wb'))
                            pickle.dump(passwordzcjm,open(path+'/密码'+passwordwj+'.txt','wb'))
                            enroll =False
                            yhzcjm =False
                            break    
    else:
        
        name=g.textbox('用户名')
        if wjs ==0:         #判断本地是否有登陆文件
            g.msgbox('错误,无用户文件')
            sys.exit()
        path=os.getcwd()
        for i in range (0,1000):    #遍历用户文件
            namewj = str(i)
            passwordwj =str(i)
            nameyz=os.getcwd() + '/用户名'+namewj+'.txt'
            passwordyz=os.getcwd() + '/密码'+passwordwj+'.txt'
            if os.path.exists(nameyz): #判断用户是否存在
                namezcjm =pickle.load(open(path+'/用户名'+namewj+'.txt','rb'))
                passwordzcjm =pickle.load(open(path+'/密码'+namewj+'.txt','rb'))
            else:
                g.msgbox('无此用户,错误!')
                sys.exit()
            namezc=decode(namezcjm)
            passwordzc=decode(passwordzcjm)
            if name == namezc:
                password =g.textbox('输入密码:')
                if password == passwordzc: #匹配存储密码
                    g.msgbox('登陆成功!')
                    yhzcjm=False
                    break
                else :
                    g.msgbox('密码错误!')
                    sys.exit()
nd = g.integerbox('是否开启限定回合制,按0取消输入任意数字及限定次数')
 #简单猜数字环节
sb=False
if nd !=0:
    cmax=nd
    a=g.integerbox('1,100猜一个数对了有糖')
    c=1
    Guessthenumbers=random.randint(1,100)
    if a < Guessthenumbers or a > Guessthenumbers:
        cmax -=1
        cs=g.textbox('猜错了呢是否开启提升按1开启任意键关闭')
        mqcs=('目前还剩下次数:'+cmax)
        g.msgbox(mqcs)
        if cs =='1':
            g.msgbox('已经开启提升')
            while cs =='1':
                if a < Guessthenumbers:
                    g.msgbox ('偏小')
                if a > Guessthenumbers:
                    g.msgbox ('偏大')
                if a == Guessthenumbers:
                    break
                cmax -=1
                sycs=('还剩下:',cmax,)
                g.msgbox(sycs)
                a=g.integerbox('继续')
                c +=1
                if cmax <=0:
                    g.msgbox('恭喜你')
                    time.sleep(3)
                    g.msgbox('次数耗尽')
                    sb =True
                    g.textbox()
                    break
                    
        else:
            while a !=Guessthenumbers:
                
                cmax -=1
                sycs=('还剩下:',cmax,)
                g.msgbox(sycs)
                a=g.integerbox('错误,继续:')
                c +=1
                if cmax <=0:
                    g.msgbox('恭喜你')
                    time.sleep(3)
                    g.msgbox('次数耗尽')
                    sb =True
                    g.textbox()
                    break
                    
        if sb ==True:
            g.msgbox('猜错了!下次再见')
        if a ==Guessthenumbers:  
            slsc=('恭喜你猜对了次数:',c,) 
            g.msgbox(slsc)
else:
    a=g.integerbox('1,100猜一个数对了有糖')
    c=1
    Guessthenumbers=random.randint(1,100)
    if a < Guessthenumbers or a > Guessthenumbers:
        cs=g.textbox('猜错了呢是否开启提升按1开启任意键关闭')
        if cs =='1':
            g.msgbox('已经开启提升')
            while cs =='1':
                if a < Guessthenumbers:
                    g.msgbox ('偏小')
                if a > Guessthenumbers:
                    g.msgbox ('偏大')
                if a == Guessthenumbers:
                    break
                a=g.integerbox('继续')
                c +=1
        else:
            while a !=Guessthenumbers:
                a=g.integerbox('错误,继续:')
                c +=1
    slsc=('恭喜你猜对了次数:',c,) 
    g.msgbox(slsc)
choisejs =g.choicebox('下次再见')

上一篇:在Markdown中使用数学公式


下一篇:LaTeX公式小结--持续更新中