Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
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.
Turn Your Knowledge into Earnings.
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: ")
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.
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.
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: ")
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.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.