Ch An Chao Sae
Ch An Chao Sae

Reputation: 39

Python get password and validate while loop

So i'm trying to ask for an input and then validate if the password is correct. It will check if the input is blank, incorrect or correct. However, after the first initial blank input, if I enter a blank again the program breaks. How do I make it keep looping and validating correctly

def getUserName():
    userName = ["Chan"]
    userNameInput = [""]
    userNameInput[0] = input("Username: ")

    while userNameInput != userName:
        if userNameInput[0] == "":
            print("Username can not be blank. Please try again.")
            userNameInput = input("Username: ")
        elif userNameInput[0] == userName[0]:
            print("Username is correct. Input password.")
        else:
            print("Username is not correct. Please try again.")
            userNameInput = input("Username: ")

Upvotes: 0

Views: 1812

Answers (3)

Rarblack
Rarblack

Reputation: 4664

Repetition in programming is a bad practice. So in my solution, I have eliminated all repetition parts from your code and

def getUserName():
    userName = "Chan"

    while True:
        userNameInput = input('Username: ')

        if not userNameInput:
            print("Username can not be blank. Please try again.")
        elif userNameInput != userName:
            print("Username is not correct. Please try again.")
        else:
            print("Username is correct")
            break;

Output:

C:\***\****>py getUserName.py
Username: sad
Username is not correct. Please try again.
Username:
Username can not be blank. Please try again.
Username: Chan
Username is correct

Upvotes: 1

Joshua Briant
Joshua Briant

Reputation: 28

The problem is that you are comparing the array index 0 however on the second time you set userNameInput it is being set to a string and not an array.

The fix would look something like this:

def getUserName():
userName = ["Chan"]
userNameInput = [""]
userNameInput[0] = input("Username: ")

while userNameInput != userName:

    if userNameInput[0] == "":
        print("Username can not be blank. Please try again.")
        userNameInput[0] = input("Username: ")
    elif userNameInput[0] == userName[0]:
        print("Username is correct. Input password.")
    else:
        print("Username is not correct. Please try again.")
        userNameInput[0] = input("Username: ")

Upvotes: 0

Gigi Bayte 2
Gigi Bayte 2

Reputation: 984

Why do you need lists to store the username and username input instead of just typical strings?

The reason the code fails with a string index out of range is that you set the userNameInput variable to a string instead of settings its first element to the string.

However, it would be preferable to just use strings instead of lists in the first place.

def getUserName():
    userName = "Chan"
    userNameInput = input("Username: ")

    while userNameInput != userName:
        if len(userNameInput) == 0:
            print("Username can not be blank. Please try again.")
            userNameInput = input("Username: ")
        elif userNameInput == userName:
            print("Username is correct. Input password.")
        else:
            print("Username is not correct. Please try again.")
            userNameInput = input("Username: ")

Here is a solution using strings instead of lists that solves your issue.

Upvotes: 2

Related Questions