Shawn Gordon
Shawn Gordon

Reputation: 173

Python - Nested IF

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

Upvotes: 0

Views: 197

Answers (1)

dhishan
dhishan

Reputation: 127

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

Upvotes: 2

Related Questions