python - Password checker program, while loops not working - python

I am making a password validator/checker program as part of my computing assignment.It must have an uppercase and lowercase letter and be at least 8 characters long.
So far I have done this:
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
while new_password != new_password2:
print("The passwords don't match up.")
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
if new_password == new_password2:
length = len(new_password)
while int(length) < 8:
print("Your password must be longer")
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
letters = set(new_password)
lower = any(letter.islower() for letter in letters)
while new_password == new_password2:
if not lower:
print("Your password must contain a lowercase letter")
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
upper = any(letter.isupper() for letter in letters)
while new_password == new_password2 :
if not upper:
print("Your password must contain an uppercase letter")
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
The code runs, but for some reason, the while loops do not work as even if the condition is right,( eg. the password contains an uppercase letter), the option for the user to enter the password again is being displayed. Can someone take a look and tell me the problem here? Thanks

You want to only have one while loop in which every requirement is checked. See the following code:
valid_password = False
while not valid_password:
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
if new_password != new_password2:
print("The passwords don't match up.")
continue
elif len(new_password) < 8:
print("Your password must be longer")
continue
elif new_password.upper() == new_password or new_password.lower() == new_password:
print("Your password must contain at least one lowercase and uppercase letter")
continue
else:
print("Password Accepted!")
valid_password = True
Hope this helps!

a= int (input("Enter Passcode: "))
if a == 1974:
print (" Welcome!! ")
else:
print ("Wrong Passcode")
print ("Run again")

Related

Python - Does not exit on the third input

This is basic python. I want the user to enter the displayed username and password 3 times. When I entered an incorrect input in the first and second tries, the code in elif functions correctly but when I incorrectly entered it in the third try, it doesn't run the statement in the elif. Can you explain why this is happening?
The code is supposed to exit when entered the wrong input. I also want to make the user enter the correct input for 3 times then it will print the welcome.
username = "Username"
password = "Password"
tries = 0
print("Your username: Username")
print("Your password: Password")
print(" ")
enterusername = str(input("Enter your username:"))
enterpassword = str(input("Enter your password:"))
while tries < 2:
if (password == enterpassword and username == enterusername):
enterusername = str(input("Enter again your Username:"))
enterpassword = str(input("Enter again your password:"))
tries = tries + 1
else:
exit("Password or username is incorrect.")
print("Welcome")
It's because you set
while tries < 2:
on the third try, tries=2, so your while loop doesn't run. Change it to while tries < 3
Your logic goes as follows:
Ask for input.
In loop:
Validate last input.
Ask new input.
You can see from this that the input form the last iteration will not be validated. You need to either:
Validate the first input before the loop and change the loop's order.
Validate the last input after the loop.
1
enterusername = input("Enter your username:")
enterpassword = input("Enter your password:")
if (password == enterpassword and username == enterusername):
while tries < 2:
enterusername = input("Enter again your Username:")
enterpassword = input("Enter again your password:")
if password == enterpassword and username == enterusername:
tries += 1
else:
exit("Password or username is incorrect.")
else:
exit("Password or username is incorrect.")
2
enterusername = input("Enter your username:")
enterpassword = input("Enter your password:")
while tries < 2:
if password == enterpassword and username == enterusername:
enterusername = input("Enter again your Username:")
enterpassword = input("Enter again your password:")
tries += 1
else:
exit("Password or username is incorrect.")
if password == enterpassword and username == enterusername:
print("Welcome")
else:
exit("Password or username is incorrect.")
I made a few small changes in your code:
input always returns a string so there is no need to convert with str().
x = x + 1 is equivalent to x += 1.
Your condition were complementary so there is no need to write the opposite in an elif - just use else.
To avoid the repeated conditions, reverse the logic: check the input in the while condition and count the tries inside the loop:
enterusername = input("Enter your username:")
enterpassword = input("Enter your password:")
while password == enterpassword and username == enterusername:
if tries == 2:
print("Welcome")
break
enterusername = input("Enter again your Username:")
enterpassword = input("Enter again your password:")
tries += 1
else:
exit("Password or username is incorrect.")
This uses the while/else construct - the else will only be executed if the loop terminated without reaching a break.
You should put
while(tries <= 2):
...
or
while(tries <3):
...

Why won't my print statements show up in my elif block?

I know there is probably a simple solution to this, but the text I wrote under the elif sections in userName and passWord will not print when a user successfully logs in. Please help!
def userName():
username = "kate"
userInput = input('Please enter your username:\n')
while userInput != username:
if len(userInput) == 0:
print("Your username can not be blank, please try again!\n")
userInput = input('Please enter your username.\n')
elif userInput == username:
print("Welcome back, " + username)
print("")
break
else:
print("Sorry, that username is not in our system. Please try again!\n")
userInput = input('Please enter your username.\n')
def passWord():
password = "stags"
passInput = input("Please enter your password:\n")
while passInput != password:
if len(passInput) == 0:
print("Your password can not be blank, please try again!\n")
passInput = input("Please enter your password.\n")
elif passInput == password:
print("You have successfully logged in!\n")
break
else:
print("Sorry, your password is invalid. Please try again!")
passInput = input("Please enter your password.\n")
def main():
print("Hello, let's get started!")
print("")
userName()
passWord()
main()
This was pointed out in the comments above, but if you change
while userInput != username:
and
while passInput != password:
to
while True:
it should work just fine. Then it forces your code to hit the elif statement rather than breaking the loop before printing what you want to say.
In python indentation indicates where a code block starts and begins. So in your while loop in passWord() your if..elif..else must be indented exactly one tab in eg.
while passInput != password:
if len(passInput) == 0:
print("Your password can not be blank, please try again!\n")
passInput = input("Please enter your password.\n")
elif passInput == password:
print("You have successfully logged in!\n")
break
else:
print("Sorry, your password is invalid. Please try again!")
passInput = input("Please enter your password.\n")
Notice how the indentation always goes one tab in for each code block you want to create.

Log in is always saying it is incorrect [duplicate]

This question already has an answer here:
Searching array reports "not found" even though it's found
(1 answer)
Closed 1 year ago.
My code is always saying it is incrorrect even tough it is present on the text file,
it was working before but now isn't for some reason.
def select_login_signup():
while True:
selection = input("Welcome to sports.com, please select"
" \"L\" to Log In with your account or \"S\" to create an account: ")
if selection.lower() == 's':
register()
answer = input("Would you like to Log In? Y/N? ")
while not answer:
if answer.lower() == "y":
login()
break
elif answer.lower() == "n":
exit()
else:
answer = False
print("Invalid answer.")
continue
elif selection.lower() == 'l':
login()
break
else:
print("Invalid answer.")
continue
def register():
username = input("Create your username (no more than 10 characters or less than 4.): ")
while 10 < len(username) < 4:
print('username cannot have more than 10 characters or less than 4.')
username = input("Create your username (no more than 10 characters or less than 4.):
")
break
while username.isnumeric():
print("username must contain at least one letter")
username = input("Create your username (no more than 10 characters or less than 4.):
")
break
password = input("Create a password with letters and numbers: ")
while len(password) < 6:
print("Your password must contain more than 6 characters.")
password = input("Create a password with letters and numbers: ")
continue
while password.isnumeric() or password.isalpha():
print("Your password must contain both letters and numbers")
password = input("Create a password with letters and numbers: ")
continue
login_credentials = open('C:\\Users\\hmarq\\Documents\\UsernameAndPassword.txt', "a")
login_credentials.write(f'\n{username},{password}')
login_credentials.close()
print("Account created successfully.")
def login() -> object:
username = input("Please enter your username: ")
username = username.strip()
password = input("Please enter your password: ")
password = password.strip()
login_credentials = open('C:\\Users\\hmarq\\Documents\\UsernameAndPassword.txt', "r")
login_credentials.readlines()
with open('C:\\Users\\hmarq\\Documents\\UsernameAndPassword.txt', 'r') as
login_credentials:
for line in login_credentials:
login_info = line.split(",")
if username == login_info[0] and password == login_info[1]:
print("Authorized")
authenticated = True
return authenticated
else:
print("Incorrect credentials.")
username = input("Please enter your username: ")
username = username.strip()
password = input("Please enter your password: ")
password = password.strip()
continue
def main():
select_login_signup()
if __name__ == "__main__":
main()
with open('C:\Users\hmarq\Documents\UsernameAndPassword.txt', 'r') as login_credentials
when you open a file, you have to format the data and therefore the data is not the same.
if username == login_info[0] and password == login_info[1]:
I hope it serves you, greetings.

Confirm user and password from a list of class objects

I need to be able to validate the user and the password inputted, but when I run the code below, I'm able to verify only the first element of the list and the second element and so on aren't being verified.
Note: The user and password are stored in the list as class objects [like this:
admin(user, password)]...
def login(self):
user_name = input("Please Enter Your Username : ").upper()
password = input("Please Enter Your Password : ").upper()
for obj in self.admins:
while obj.admin_name != user_name and obj.admin_password != password:
print(" Sorry Username and Password Incorrect Please Re-enter for Validation ")
user_name = input("Please Enter Your Username : ").upper()
password = input("Please Enter Your Password : ").upper()
else:
print("Greetings,", user_name, "You are Now Logged in the System")
break
When you run break in your else branch you are actually calling it on the for loop. Remove the break and it should be working as you expect it to
Your while loop only checks for the first user name. You should switch the order of your loops:
def login(self):
user_name = input("Please Enter Your Username : ").upper()
password = input("Please Enter Your Password : ").upper()
while True:
for obj in self.admins:
if obj.admin_name == user_name and obj.admin_password == password:
break
else:
print(" Sorry Username and Password Incorrect Please Re-enter for Validation ")
user_name = input("Please Enter Your Username : ").upper()
password = input("Please Enter Your Password : ").upper()
continue
break
print("Greetings,", user_name, "You are Now Logged in the System")
This is also a very bad way to check passwords.
Simply remove break from your else statement.

When I enter my username, "good job" isn't being displayed

import time
def Scenario2():
film_comedy = ['Anchorman','The Hangover','Ted']
film_horror = ['The Exorcist','The Shining','Scream']
film_action = ['Die Hard','The Matrix','Batman']
film_animation = ['Toy Story','The Incredibles','The Lion King']
print("Hello, welcome to the online streaming service")
username = len(input("Please Enter a username between 4 and 12 characters: "))
while username < 4 or username > 12:
print("That username is not within the boundaries")
username = len(input("Please Enter a username between 4 and 12 characters: "))
password = input("Now enter a password: ")
password1 = input("Please re-enter the password: ")
if password1 == password:
print("Congratualtions on your new account")
while password1 != password:
print("They don't match")
password = input("Now enter a password: ")
password1 = input("Please re-enter the password: ")
if password1 == password:
print("Congratualations on your new account")
usernameinput = input("Please Enter your username: ")
if usernameinput == username:
print("Great job")
while usernameinput != username:
print("That is the incorrect Username")
usernameinput = input("Please Enter your username: ")
if usernameinput == username:
print("Great job")
Scenario2()
Whenever I enter the usernameinput the same as username. it doesn't say good job but that it is incorrect.
Could I get some help with this?
while usernameinput != username:
print("That is the incorrect Username")
usernameinput = input("Please Enter your username: ")
if usernameinput == username:
print("Great job")
This won't work because, you have assigned username = len(input("Please Enter a username between 4 and 12 characters: ")) before this while loop, rename your variables for this to work.
Username is being assigned as the length of the input, not the input. You need to introduce a separate variable to assign the input too.

Categories

Resources