beginner programmer with python here. I'm wondering why my "attempt" counter only updates once despite the loop continuing.
def login():
attempt = 3
username = input("Enter your username:\n> ")
while True:
if username in userNamePassword:
password = input("Enter your password:\n> ")
if userNamePassword[username] == password:
print("Logged in")
return True
elif userNamePassword[username] != password:
attempt -=1
print("Sorry, the password is invalid. You have", attempt,"attempts remaining.")
login()
if attempt == 0:
print("Sorry, you have exhausted your password attempts. Please restart the process.")
exit()
It only ever prints 1 as the attempt remaining however continues the loop asking for the user and pass once more. It's possibly a small, overlooked issue but I'm scratching my head here. Thanks
Remove that login() call. When you call the function, the attemps will reset to 3. If you don't call it, you will stay in loop and the attemps will decrement. I recommend you, instead of exit() to use break
Related
Im new to python and I'm trying to code a python login program. Instead of printing out "Welcome to the database" when the username provided is correct, it printed out both "Welcome to the database" and "Username invalid. Please try again.". May I know which part of my code needs to be corrected?
def login():
while True:
name = input("Name: ")
with open('username.txt', "r")as name_file:
for line in name_file.readlines():
if name == line.strip():
print("welcome to database")
else:
print("Username invalid. Please try again")
You are looping through all the users in the text file and for each of them printing to the console. The thing you probably want could be done like this:
def login():
while True:
loginSucessful = False
name = input("Name: ")
with open('username.txt', "r")as name_file:
for line in name_file.readlines():
if name == line.strip():
loginSucessful = True
break
if loginSucessful:
print("welcome to database")
else:
print("Username invalid. Please try again")
You could use a boolean variable to keep track of successful logins like #Michalor did. Or you can use Python's for/else loop to do the same thing without adding a new variable. If the login is successful and you break out of the loop, the "else" statement isn't executed. Using "break" also has the advantage that you don't need to test all of the other users after you have found a successful login.
def login():
while True:
name = input("Name: ")
with open('username.txt', "r")as name_file:
for line in name_file.readlines():
if name == line.strip():
print("welcome to database")
break
else:
print("Username invalid. Please try again")
Of course, this kind of function doesn't provide much security, as you can keep guessing the names in the text file until you find a valid one, or if you can get your hands on the text file itself you can just look the names up. For actual login code, it's probably best to use some kind of login library that handles the security details for you.
so i have this code
pa=""
newpa=""
use=''
use=input('enter username:')
pa=input('enter password')
while True:
if len(pa)==12:
newpa=input('re-enter password:')
else:
print('the password used did not meet our system requirements, please enter a 12 digit password')
if newpa==pa:
print('you have successfully created a new account!')
whenever I insert a pa that's incorrect the else print keeps looping. How do I make it so whenever I enter a pa that doesn't meet the if to loop back to newpa
if this help this is my algorithm alogrythm of what I am trying to go for
First thing to do would be the put the input statement inside the while loop. At the moment your code loops back to the top of the while loop without giving the user a chance to re-enter their password.
You'll also want to move the check for the re-entry part so that it only executes if the first password was valid. And let the user know if their passwords didn't match.
Finally, you'll want to break the loop when a valid password is entered and verified. Use break for this.
The final code would look like this:
pa=""
newpa=""
use=''
use=input('enter username:')
while True:
pa=input('enter password')
if len(pa)==12:
newpa=input('re-enter password:')
if newpa==pa:
print('you have successfully created a new account!')
break
else:
print('the passwords did not match!')
else:
print('the password used did not meet our system requirements, please enter a 12 digit password')
Hope that helps and makes sense!
Your loop will run continuously until you add break statement.
You can try something like below,
pa=""
newpa=""
use=''
use=input('enter username:')
while True:
pa=input('enter password')
if len(pa)==12:
newpa=input('re-enter password:')
if newpa==pa:
print('you have successfully created a new account!')
break
else:
print('the password used did not meet our system requirements, please enter a 12 digit password')
When you enter in the last if you want to exit from loop. So, add break after print.
if newpa==pa:
print('you have successfully created a new account!')
break
Use a while True loop and break from that loop if the password meets the requirements. Then you can enter the password a second time and compare if both inputs match.
username = input('enter username:')
while True:
password = input('enter password')
if len(password) == 12 and password.isdigit():
break
print('the password used did not meet our system requirements, please enter a 12 digit password')
password_check = input('re-enter password:')
if password == password_check:
print('you have successfully created a new account!')
You might want to wrap that in an additional loop if the passwords don't match.
If you use functions you can reduce the complexity of the main part.
def enter_password(message):
while True:
password = input(message)
if len(password) == 12 and password.isdigit():
break
print('the password used did not meet our system requirements, please enter a 12 digit password')
return password
def main():
username = input('enter username:')
while True:
password = enter_password('enter password')
password_match = enter_password('re-enter password')
if password == password_match:
break
print("The passwords don't match.")
print('you have successfully created a new account!')
if __name__ == '__main__':
main()
Please use break to break the statement and continue to continue the loop statement according to your requirement. I hope the following code meets your requirement. Thanks and let me know if there's anything I can help with.
user=input('enter username:')
while True:
pa=input('enter password:')
if len(pa)==12:
newpa=input('re-enter password:')
while newpa!=pa:
newpa=input('re-enter password:')
else:
print('the password used did not meet our system requirements, please enter a 12 digit password')
continue;
if (newpa==pa):
print('you have successfully created a new account!')
break;
Please, I need help with this login system that I am currently working on, but I keep on getting the indented block error at def password(). I'm new to python but have worked with Java for a few years.
I have tried to indent the def password(), but it just keeps on popping up.
def username():
userinputname = input('Please enter the username')
if userinputname == user:
password()
elif userinputname != user:
print('Wrong username, please try again')
username()
else:
# problems()3
def password():
userinputpass = input('Please enter the password')
if userinputpass == passs:
main()
elif userinputpass != passs
for x <=5:
print('Wrong password, please try again')
x++
exit
else:
# problems()
def register():
registeragain = False
user = input('Please register your username')
passs = input('Please register your password')
register = input('Do you want to register another account? Y/N')
if register == 'Y':
registeragain = True
elif register == 'N':
username()
else:
print("Error")
# problems()
register()
I ran the problem and the error showed expected an indented block (, line 12)
else:
# problems()3
def password():
The expected indented block error happens because you're supposed to have an indented block of code following an else:. Instead, you go directly from there to an unindent (starting a new function definition).
If you put a pass statement under your else that will satisfy the requirement for there to be a block under it. Alternatively you could just remove the else since it's not doing anything.
You've declared an else statement at the end of each function without including anything to execute within the statement. Else statements expect an indented block of code, hence the problem.
If your intention is to do nothing in an 'else' block, use a 'pass' statement.
else:
pass
# problems()
Instead of below, which is causing problems.
else:
# problems()
I am self-teaching myself python and have run into a problem that I can not seem to find a way around.
I have created a piece of code that compares an entered password to one stored in a database.
My code should have two possibilities.
1) If the password is correct.
The user is prompted to enter a new password and then the prompt to enter the password must appear again (This time accepting the new password).
2)If the password is incorrect the user will be prompted to enter the password until the correct password is entered.
In VBS I used to be able to use the GOTO command.
I am not sure if this is available in Python and if it is I would like to avoid using it as it creates a very illogical hard to follow the program.
password = "#123"
entry = input("Please Input The Password:")
if (password == entry):
entry = input("Password correct you may enter a new password.")
else:
entry = input("Password Incorrect, Try again.")
There are various ways you could complete this. Here is a simple way you could achieve it using while loop and break statement.
password = "#123"
while(True):
entry = raw_input("Please Input The Password: ")
if (password == entry):
print("Password correct you may enter a new password.")
break
else:
print("Password Incorrect, Try again.")
Hope it helped.
while password != entry: # Executes until (password == entry), and does not execute if it is met, even for the first time.
print('Sorry, wrong password.')
entry = input('Enter password >') # or other source of data
print('Correct!')
Edit: additional ways you can do this:
while True: # forever loop, but
entry = input('Enter password >') # or other source of data
if password == entry:
print('Correct!') # you can also put this outside of the loop
break # exit the loop no matter what
# do not put code after the break statement, it will not run!
print('Sorry, wrong password') # will execute only if password != entry, break ignores the rest of the code in the loop
Easiest to make a function with a while statement.
password = "#123"
def login():
while True:
answer = input("Please Input The Password:")
if answer == password:
print("Password correct you may enter a new password.")
else:
print("Password Incorrect, Try again.")
break
login()
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Checker = 'Incorrect'
name = input('Please Enter Your Name: ')
password = input('Please Enter Your Password: ')
if password == 'badger123':
print('Checking Password...')
else:
print(Checker)
if Checker == 'Incorrect':
exit()
import time
time.sleep(5)
print('Password is Correct')
time.sleep(3)
input('Press Enter to Continue...')
print ('Hello' , name)
import time
time.sleep(1)
print ("Closing Python...")
import time
time.sleep(5)
input('Press Enter to continue...')
exit()
I need help , the correct password is badger123 but even if I enter the correct password it will print 'password is correct' and then exit but I want it to ignore exit and continue. When the wrong password is entered then the program will print incorrect and will exit. The programs reacts correctly when the incorrect password is entered but not when the correct password is entered. I NEED HELP!
The value of Checker never actually changes, so
if Checker == 'Incorrect':
exit()
always evaluates to True, which means exit() is always called.
One solution is to do the checking in the if statement
import time
password = input('Please Enter Your Password: ')
if password == 'badger123':
print('Password is Correct...')
else:
print('Password Incorrect')
exit()
time.sleep(3)
input('Press Enter to Continue...')
print ('Hello' , name)
time.sleep(1)
print ("Closing Python...")
input('Press Enter to continue...')
First you only need to import a module once (by convention at the top of your file), and not each time you want to use a function.
The problem you describe is impossible with the code you show
Checker = 'Incorrect'
if Checker == 'Incorrect':
exit()
Since you assign 'Incorrect' to Checker and never changes it after that, the rest of your code is not executed, and your program will never print 'password is correct', it will however print 'Checking Password...' when the correct password is entered and then exit because Checker is always the same, it's like doing if 1 == 1:exit()
Why it isn't working
You set value checker to "incorrect"
When the password is found to be correct, nothing is done about it. Nothing notifies the program in any way that the correct password has been entered. You can see in program that all the program does to acknowledge that the password is correct is simply say that it is "checking" and nothing else. 3 lines later the program will find that checker is still "incorrect" because the program, seeing that the password is correct does nothing to change checker so line 9 is inevitably going to happen.
Fix:
Put line 10 with the else statement and remove line 9. So your code will look something like.
password = input('Please enter your password: ')
if password == 'badger123':
print('Checking Password...')
else:
print(Checker)
exit()
I can suggest an improvement.
password = input('Please enter your password: ')
if not password == 'badger123':
print(Checker)
exit()
print('Checking Password...')
I think this would be the ANSWER to my question! , All I did is I changed the value of Checker. But now when the person gets the password wrong I want the program to automatically Restart . How do I do that?
Checker = 'Incorrect'
name = input('Please Enter Your Name: ')
password = input('Please Enter Your Password: ')
if password == 'badger123':
print('Checking Password...')
else:
print(Checker)
Checker = exit()
if Checker == 'Incorrect':
input(Checker)
import time
time.sleep(5)
print('Password is Correct')
time.sleep(3)
input('Press Enter to Continue...')
print ('Hello' , name)
import time
time.sleep(1)
print ("Closing Python...")
import time
time.sleep(5)
input('Press Enter to continue...')
exit()