如何在用户在python中创建用户名/密码之前保持代码块循环?

时间:2017-02-22 18:18:45

标签: python python-2.7

我有这段代码:

while True:

uName = []
uPassword = []

maxLengthList = 1
maxPassList = 1

acceptEnt = ["YES", "yes", "Y", "y"]
denyEnt = ["NO", "no", "N", "y"]

while len(uName) < maxLengthList:
    item = raw_input("Create a username: ")
    uName.append(item)
    checkU = raw_input("Is %s correct? " % uName)
    if checkU in acceptEnt:
        while len(uPassword) < maxPassList:
            pw = raw_input("Create a password: ")
            uPassword.append(pw)
            checkP = raw_input("Is %s correct? " % uPassword)
            if checkP in acceptEnt:
                print "%s logging in..." % uName
    else:
        if checkU in denyEnt or checkP in denyEnt:
            print "Error"
break

应该让用户创建用户名和密码,然后传递给:

done = True

while done == True:
    sys.stdout.write('\rLoading')
    time.sleep(0.11)
    sys.stdout.write('\rLoading. ')
    time.sleep(1)
    sys.stdout.write('\rLoading.. ')
    time.sleep(2)
    sys.stdout.write('\rLoading... ')
    time.sleep(1)
    sys.stdout.write('\rLoading.... ')
    time.sleep(0.39)
    sys.stdout.write('\rLoading..... ')
    time.sleep(0.19)
    sys.stdout.write('\rLoading...... ')
    time.sleep(0.25)
    done = False
sys.stdout.write('\rInitializing')
time.sleep(3)
sys.stdout.write('\rHello, %s' % uName)
time.sleep(1.5)

仅模拟加载操作。在用户同时输入用户名和密码之前,我需要它不传递给该代码块,但即使用户输入&#34; no&#34;当询问%s是否正确时,它会传递给下一个代码。

我试图让它坚持创建帐户,直到输入真正的输入,并且当&#34; no&#34;输入,然后将用户返回到上一个创建屏幕。我根本不希望他们能够绕过那部分,这就是他们能做到的。

更新:

我最终将它更改为布尔值,虽然我打算稍微清理一下代码,但对我来说似乎更快。

entry = True

while entry == True:

uName = []
uPassword = []

maxLengthList = 1
maxPassList = 1

acceptEnt = ["YES", "yes", "Y", "y"]
denyEnt = ["NO", "no", "N", "y"]

while len(uName) < maxLengthList:
    item = raw_input("Create a username: ")
    uName.append(item)
    checkU = raw_input("Is %s correct? " % uName)
    if checkU in acceptEnt:
        while len(uPassword) < maxPassList:
            pw = raw_input("Create a password: ")
            uPassword.append(pw)
            checkP = raw_input("Is %s correct? " % uPassword)
            if checkP in acceptEnt:
                print "%s logging in..." % uName
                entry = False
                break
            else:
                print "pw wrong test"
    else:
        print "uname wrong test"
    break

这似乎暂时有用,虽然我会在寻找替代方案时再次访问它,例如这里提到的替代方案。

2 个答案:

答案 0 :(得分:0)

你能不能实现这是一种完全不同的方式,比如使用事件处理程序? 代码执行完成,用户显示文本框,填写文本框并按下按钮,执行恢复,代码继续 要使用的好事件系统是http://pydispatcher.sourceforge.net/

这将允许您等待事件,并在事件发生时以编程方式处理事件

答案 1 :(得分:0)

你可以用函数和递归来做,我觉得以下是非常干净的(尽管它可能会变短)。

CODE:

import time
import sys

def pause_clear(message='Loading',delays=None):
    delays = delays if delays else [0.11,1,2,1,0.39,0.19,0.25]
    for ind,delay in enumerate(delays):
        sys.stdout.write(message+'.'*(ind+1)+'\r')
        time.sleep(delay)
        sys.stdout.flush()
    sys.stdout.write('\n')

def ask_ok():
    response = raw_input('Is this correct? ')

    acceptEnt = ['YES','yes','Y','y']
    denyEnt = ['NO','no','N','n']
    if response in acceptEnt:
        return True
    elif response in denyEnt:
        return False
    else:
        pause_clear('only yes or no answers please',[0.1,2])
        return ask_ok()

def prompt(question):
    answer = raw_input(question+' ')
    if ask_ok():
        return answer
    else:
        pause_clear('ok then please try again',[0.1,2])
        return prompt(question)


uName = prompt('What is your username? ')
pause_clear()
uPassword = prompt('What is your password? ')
pause_clear()
sys.stdout.write('Done, your username is ['+uName+'] and password is ['+uPassword+']\n')