0

I have this code:

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

that is supposed to have a user create a username, and password, then it passes to this:

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)

which only simulates a loading operation. I need it to not pass to that code block until a user enters both a username and password, but even if the user inputs "no" when asking if %s is correct, it passes to the next code.

I am trying to get it to stick to the account creation until a real input is entered, and when "no" is input, then throw the user back to the previous creation screen. I don't want them to be able to bypass that part at all, which is what they can do.

UPDATE:

I ended up changing it to a boolean, seemed quicker to me though I plan on cleaning the code up a little bit.

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

This seems to work for the time being though I will most definitely visit it again as I look for alternatives such as the ones mentioned here.

T. Duke
  • 646
  • 1
  • 6
  • 17
  • 2
    Not, strictly speaking, a duplicate, but [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658/953482) may give you some useful ideas. – Kevin Feb 22 '17 at 18:31
  • 1
    Possible duplicate of [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Prune Feb 22 '17 at 18:43

2 Answers2

0

Could you not implement this is an entirely different way, such as using event handlers? Code execution finishes, user is shown text boxes, when the text boxes are filled in and a button pressed, execution resumes and your code continues A good event system to use would be http://pydispatcher.sourceforge.net/

This would allow you to wait for an event, and handle the event programmatically when it does occur

kxdan
  • 187
  • 1
  • 15
  • 1
    I will look into that, though the program is mostly text-based. This is the core code which is creating a virtual PC environment. I've come a long way, but I'm still learning. This is my pet project. – T. Duke Feb 22 '17 at 18:41
0

You can do it with functions and recursion, I feel like the following is pretty clean (although maybe it could be made shorter).

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')
mitoRibo
  • 4,468
  • 1
  • 13
  • 22
  • Definitely looks good. Check my edit in my original post, I ended up just adding a boolean to it and it works for the time being. I will use this as a reference later when I revisit it to change it. Thanks! – T. Duke Feb 22 '17 at 19:06