This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 5 years ago.
I'm trying to get the user to enter a password (when making an account for my quiz) and check that the password is over 6 characters. If it isn't, it will loop back and ask the user to try another password. Here's what I've got so far:
def password ():
password = input("enter a password over 6 characters")
count = 0
for letter in password:
count = count + 1
while count < 6:
password = input("password too short try again")
password ()
Here's what I get if my password is long enough:
enter a password over 6 characters longpassword
Here's what I get if my password's too short:
enter a password over 6 characters lol
password too short try again lol
password too short try again longpassword
password too short try again
It doesn't seem to count the characters after the first time and I don't know why.
password = ""
while len(password) < 6:
password = input("....")
Related
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 3 months ago.
Right now with what I have, even if the password meets all the criteria it prints "Weak password try again!". It allows for another user input but it doesn't break and print "Strong password" if it is strong.
Code:
if (l>= and u>=1 and p>=1 and d>=1 and l+u+p+d==len(s)):
break
print("Strong password")
else:
print(input("Weak password, try again: "))
while True:
passwordName = input("Password ? ")
if (l>= and u>=1 and p>=1 and d>=1 and l+u+p+d==len(s)):
print("Strong password")
break
else:
print("Weak password, try again")
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 2 years ago.
I'm very new at Python, and I'm trying to make a simple program that asks the user for a password, and if the user didn't guess the password they were asked, ask again until they guess it. How do I do that?
Password = input("guess the password: ")
while (password) != "12345":
Print(input("try again : "))
Welcome to Programming and StackOverflow. Have a look a this Example,
n = 5
while n > 0:
n -= 1
if n == 2:
break
print(n)
print('Loop ended.')
The break statement ends the while loop.
#g23's Answer is more apt in the context of the question
Make sure your capitalization is right, generally variables are lowercase, but you need to be consistent.
Also when you ask for the password again, you need to store what the user gives you so it can be checked in the loop condition (the while password != "12345": part)
Something like
password = input("Enter the password: ")
while password != "12345":
password = input("try again: ")
This code does what you want. It has a while loop to check if the password was guessed until the right password is entered. Then it has an if statement to write a message: if the right password is entered, it writes it.
password = input("Enter the password: ")
while password != "12345":
password = input("try again: ")
if password == "12345":
print("Correct password!")
I am studying Python for a little while and trying to resolve following problem:
Part of verification routine is given below. When the user enters the password, it is checked to ensure it is between 8 and 15 characters and then requested to verify the password by typing it again. Require complete task(a) CONDITION 1, CONDITION 2, and task (b) IF ... ELSE statement. Code and test the program.
password1 = input("Please enter a new password, between 8 and 15 characters: ")
match = False
while CONDITION 1
while CONDITION 2
password1 = input ("Password must be between 8 and 15 characters - please re-enter: ")
endwhile
password2 = input ("Please verify password: ")
if ....
print
else
endif
endwhile
I complete the task (b) but in task (a) I have a problem with while loop condition "while match:". When I leave variable match = False and enter the password with required amount of characters the loop terminates straight away and does not do verification with if .. else statement. If I write match = True and on first attempt enter incorrect number of characters (with correct number it terminates the loop again) program goes for verification anyway ignoring the number of characters. If the password was the same (example 111 and 111) it goes back to the loop and asks again for the password with required amount of characters. Then if I enter after the right amount of characters and password verification is the same it terminates the program like it suppose to be. I think something with the condition "match" in while loop is not right but can't figure out what it should be. Thanks
password1 = input("Please enter a new password, between 8 and 15 characters: ")
match = True
while len(password1) < 8 or len(password1) > 15:
password1 = input ("Password must be between 8 and 15 characters - please re-enter: ")
while match:
password2 = input ("Please verify password: ")
if password1 == password2:
print ("Password is valid")
break
else:
print ("Password is invalid")
I think like myself you are new to this. I find the following wrong with your code interpretation of the problem:
Condition 1 and 2 are misplaced
If statement is in the wrong loop.
Your match is also not switched.
Check this out:
def password_verification():
password1 = input('Please enter a new password, between 8 to 15 characters: ')
match = False
while match is False:
while len(password1) < 8 or len(password1) > 15:
password1 = input('Password must be between 8 to 15 characters - please re-enter password: ')
password2 = input('Please verify password: ')
if password1 == password2:
print('Password is valid')
match = True
else:
print('Password is invalid')
return match
password_verification()
You are matching only when the first time wrong number of characters were entered. What will happen if in the first time, user enters between 8 to 15 characters? The first while loop will not execute, and hence nothing will execute. Condition1 (length between 8 and 15) must be checked outside the loop, and the second while loop must be kept separate.
pass1 = input('Enter a password : ')
while len(pass1) < 8 or len(pass1) > 15:
print('Incorrect, try again')
pass1 = input('Enter a password : ')
# Program reaches here only if pass1 is between 8 to 15 characters, otherwise first loop will be infinite
pass2 = input('Verify the password : ')
if pass1 == pass2:
print('Valid')
else:
while pass1 != pass2:
pass2 = input('Invalid. Re-enter the correct password : ')
The above code first takes password1, checks if it is within the 8-15 characters range. If it is not, the first while loop keeps on executing. When a valid input is given, it takes password2, and checks for equality. If it matches for the first time, no further while loop is needed. If it doesn't match, the while loop keeps on executing till equal passwords are not provided. Hope this helps!
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 7 years ago.
import re
username = input("Enter your name to start the test: ")
#Prompting the user to input their name
valid = re.match("^[A-Za-z]*$",username)
#A validation of letters only being used
if not valid:
print("Error! Letters only!")
username = input("Enter your name: ")
why not use a while loop with your condition. Something like this, you fill in the details:
username = input("Enter your name to start the test: ")
while not re.match("^[A-Za-z]*$",username):
print("Error! Letters only!")
username = input("Enter your name: ")
Try this:
import re
valid = False
while not valid:
username = input("Enter your name to start the test: ")
valid = re.match("^[A-Za-z]*$", username)
if not valid:
print("Error! Letters only!")
I came across a problem within my program that I'm writing. I've narrowed down the problem to these two functions. The problem occurs when you call the function enterPasswords, enter invalid data such as 'a', then break out of the passwordLength function by entering valid data such as 'hello'. I've left some print statements there to help you see the problem. I've tried adding returns, but the same problem still occurs.
Any advice will be greatly appreciated. If you could tell me why the problem is occurring, I'm sure I could fix it myself. Thanks.
def passwordLength(password):
if (len(password) < 4) or (len(password) > 15):
print("Error from server: Your password must be at least four and at most fifteen characters long.")
enterPasswords()
def enterPasswords():
password = input("Input password: ")
passwordLength(password)
print(password)
password2 = input("Re-enter password: ")
print(password, password2)
enterPasswords()
Here is an image of my problem (What I'm wanting to know is, why isn't the program ending where I've highlighted, why does it carry on, and why is 'a' being printed towards the end?):
http://i.imgur.com/LEXQFTO.png
It appears that if the user inputs an invalid password at first, it repeats enterPasswords - however, if the user completes this successfully, it goes back to the initial enterPasswords. Instead, try
def passwordLength(password):
if (len(password) < 4) or (len(password) > 15):
print("Error from server: Your password must be at least four and at most fifteen characters long.")
return False
return True
def enterPasswords():
password = input("Input password: ")
while not passwordLength(password):
password = input("Input password: ")
print(password)
password2 = input("Re-enter password: ")
print(password, password2)
This will continue to ask the user to reinput the first password until it is valid, and only then it will ask the user to confirm.
The password variable in passwordLength() is completely unrelated to the variable in enterPasswords(). The behaviour might also not be as you expect it to be. Try something like this:
def passwordLength(pw):
return 4 <= len(pw) <=15
def getPw():
return input("Enter password: ")
def enterPasswords():
pw = getPw()
while not passwordLength(pw):
print("Incorrect password length.")
pw = getPw()
# ...
Your functions are calling each other in a bad way. If you try to follow line by line your algorithme (using the case you mentioned with 'a' and 'hello') you will probably see the problem.
Here is a solution :
def passwordLength(password):
if (len(password) < 4) or (len(password) > 15):
print("Error from server: Your password must be at least four and at most fifteen characters long.")
return False
else : return True
def enterPasswords():
passwordOK = False
while not passwordOK :
password = input("Input password: ")
passwordOK = passwordLength(password)
print(password)
password2 = input("Re-enter password: ")
print(password, password2)
enterPasswords()