Python get password and validate while loop

General Tech Bugs & Fixes 2 years ago

0 3 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (3)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

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: ")
profilepic.png
manpreet 2 years ago

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.

0 views   0 shares

profilepic.png
manpreet 2 years ago

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: ")

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.