0

Like many here, I'm new to Python. I'm working on a snippet that asks the user to give their ID, then checks to see if the ID is exactly 6 digits in length. Then the code will ask the user to confirm their ID and if they mistyped, allows them to reset it. If the user confirms their entry was correct, then it asks for a location ID and follows the same path. If both IDs are confirmed, the user then can move on to the rest of the project.

This is something that will have to be input at the start of every use.

The issue I'm running in three sided.

1.) I can enter the empID 101290 and sometimes it tells me it's a valid entry while others it wont (but 101256 works regardless - both are 6 digits)

2.) Entering "1," to confirm the ID, the code moves to block 2 and asks for location ID but if the user enters "2" to say the Employee ID is wrong, it moves on anyway.

Any advice on what's in need of change here?

import time
print('What is your employee ID?') #user assigned ID
empID = input()
while empID != 0:
    print('Try again.')
    empID = input()

# employee ID is only 6 digits in length, no letters
    if len(empID) != 6:
        print('Try again.')
    elif len(empID) == 6:
        print('Thank you. Your ID is set to ' + empID + '.')
        time.sleep(.5)
        print('Is this correct?'''
              '[1] Yes  [2] No ')
        yesNo = input()
        while True:
            yesNo == '1'
            print('Thank you. ID set.')
            break
# reset ID
        else:
            print('ID has been reset. Please enter your employee ID.')
            empID = input()
            break
    break

#Store Location ID
print('What is your Location ID?')
locID = input()
while locID != 0:
    print('Try again.')
    locID = input()

# store locations are 3-5 digits
# TODO: prepend any input with less than len 5 with 0
    if len(locID) != 5:
        print('Try again.')
    elif len(locID) == 5:
        print('Thank you. Your location is set to ' + locID + '.')
        time.sleep(.5)
        print('Is this correct?'''
              '[1] Yes  [2] No ')
        yesNo = input()
        while True:
            yesNo == '1'
            print('Thank you. Location ' + locID + 'set.')
            break
        else:
            print('Location ID has been reset. Please enter your location code.')
            empID = input()
            break
        break
    break
#next
6
  • if loops don't exist. Commented Dec 26, 2016 at 23:20
  • Your indentation is wrong, the code here in the question isn't valid syntax. Unless you fix it we can't help. Edit your question, copy the code into the question again, select all of it, and click the curly braces button to indent it (thus formatting it as a code block). Commented Dec 26, 2016 at 23:25
  • Ok, then I called it something wrong. Commented Dec 26, 2016 at 23:25
  • Thank you Alex, I think I fixed the formatting. Sorry. Commented Dec 26, 2016 at 23:28
  • At a glance, I'm guessing that instead of while True: yesNo == '1' you want if yesNo == '1', and without the break statements. But, I haven't tested whether this fixes the issues you listed. Commented Dec 26, 2016 at 23:39

1 Answer 1

2

I see some Bugs in your code to start with.

while True:
    yesNo == '1' 

yesNo == '1' is a condition statement which returns true or false depending on the user input, but it is not used in as a condition anywhere

if len(empID) != 6:
    print('Try again.')
elif len(empID) == 6: 

`elif len(empID) == 6:` is redundant.. a simple else will do

What I would do is: Define functions to validate the user credentials:

def isEmpID(id):
    '''
    Employee ID is 6 characters in Length
    '''
    if len(id) != 6:
        return False
    return True


def isStoreID(id):
    '''
    Store ID is 3-6 characters in Length
    Note: The function when called with id, checks if the length is between (exclusive) 3 and (inclusive) 6 and returns true if condition is satisfied else false which is the default return policy
    '''
    if 3 < len(id) <= 6:
        return True
    return False


validEmpID = False
validStoreID = False
while not (validEmpID and validStoreID): # Both has to be True to exit the loop, Otherwise the condition continues to go to True.
    if not validEmpID:
        print('Enter Employee ID:')
        empID = input()
        validEmpID = isEmpID(empID)
        if not validEmpID:
            print('Invalid Employee ID\nTry Again...\n')
            continue
    print('Enter Store ID:')
    strID = input()
    validStoreID = isStoreID(strID)
    if not validStoreID:
        print("Invalid Store ID\nTry Again!...\n")
        continue

Here the loop exists or in other words continue executing the code afterwards only if both the variables are True

Sign up to request clarification or add additional context in comments.

4 Comments

I appreciate the insight and corrections. It continues to hang and give me a syntax error unless I drop a next between def blocks and it expects an indent when I change the elif to the the suggested else.
next() is a function used for iterators. You don't need it anywhere in your code. Also else is only used with if not with while.
Do post your edited code above. Let me have a look at it
done. I do appreciate looking at it. I wont be doing more with it for a few hours - have to get kids in bed and all that fun dad stuff :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.