from pad4pi import rpi_gpio
# Setup Keypad
KEYPAD = [
["1","2","3","A"],
["4","5","6","B"],
["7","8","9","C"],
["*","0","#","D"]
]
ROW_PINS = [5,6,13,19] # BCM numbering
COL_PINS = [26,16,20,21] # BCM numbering
factory = rpi_gpio.KeypadFactory()
keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS)
def processKey(key):
print("enter 3 digit")
print(key)
if key == 123:
print("correct")
else:
print("wrong password")
keypad.registerKeyPressHandler(processKey)
I want the code to wait for the user enter for example 3 digits before comparing with the password in the code which is 123 in above code.
What it should do:
Wait user enter 3 digit from the keypad , for example 123 , then print correct.
What it actually do:
it will print correct or incorrect password straight away after user enter 1 digit code
Update for raspberry taking #furas example:
# Initial keypad setup
code = ''
def processKey(key):
print("Enter your 3 digit PWD: \n")
global code
MAX_ALLOWED_CHAR = 3
code += key
if (len(code) == MAX_ALLOWED_CHAR):
if (code == "123"):
print("You entered the correct code.")
dostuff()
else:
code = ''
print("The passcode you entered is wrong, retry.")
def dostuff():
# do your things here since passcode is correct.
This might do it for your case.
def processKey():
key = input("enter 3 digit")
if (key == "123"):
print("Correct password.")
return True
else:
print("You typed {0} wich is incorrect.".format(key))
return False
So now you don't give processKey a value because as you said the user input it, calling processKey() will ask the user to enter a password and return true/false based on the "123" in the check.
This is if you want to input the password, but if the following answer is not suiting your needs (didn't quite fully understand what you want to accomplish) just provide more clever example.
Edit:
Since you wanted to strictly have a 3 digit input and a re-input password in case they enter the wrong one you can do the following:
On the call to processKey() you can:
while (processKey() == False):
processKey()
Revisioned code to match your needs:
def processKey():
MAX_ALLOWED_CHAR = 3
key = input("Enter 3 digit PWD: \n")
if (key == 123):
print("Correct password.")
return True
elif (len(str(key)) > MAX_ALLOWED_CHAR):
print("The max allowed character is {0}, instead you entered {1}.".format(MAX_ALLOWED_CHAR,key))
return False
else:
print("You typed {0} wich is incorrect.".format(key))
return False
while (processKey() == False):
processKey()
Output:
Enter 3 digit PWD:
3333
The max allowed character is 3, instead you entered 3333.
Enter 3 digit PWD:
321
You typed 321 wich is incorrect.
Enter 3 digit PWD:
123
Correct password.
keypress is executed after every key press - and it is natural. You have to keep all keys on list or in string and check its length.
code = ''
def processKey(key):
global code
code += key
if len(code) == 3:
if code == "123":
print("correct")
else:
print("wrong password, try again")
code = ''
Related
I am creating a program that checks passwords to see if they are strong enough:
they need caps,
lower case,
numbers,
symbols and
cannot contain easy passwords.
As you can see below this is only some of my code and I have a menu, and a password checker, the only problem is that the checker doesn't seem to be working as when I enter a password, its always saying its a strong password even though it does not contain any caps or numbers or symbols. Below is my code so far.
import random #importing random
import time #importing time
global Menu
def Menu():
global optionchoice #globalising variable optionchoice so that it can be used inside of another def of the code
print("Hey, Welcome to PassWordChecker.")
time.sleep(2) #delaying the next line for 2 seconds
print("Please choose one of the options below." )
time.sleep(1)
print("MENU:")
print("***")
print(" press 1 once for : password checker ")
print(" press 2 twice for : password generator")
print(" press 3 two or three times to : Quit ")
print(" ***") #creating a basic menu
optionchoice = (input("which option would you like to choose?"))
def checkpasswordandinput():
global optionchoice
optionchoice = optionchoice
if optionchoice == '1':
print(" please enter a password. ")
UserPassword = input("")
if len(UserPassword) <= 8 or len(UserPassword) >= 24 or UserPassword == UserPassword.isupper() or UserPassword == UserPassword.isdigit() or UserPassword == UserPassword.islower() or UserPassword == UserPassword.isalpha():
print("make sure your password includes numbers and upper and lower case letters ")
UserPassword = input('please enter a new password')
else:
print('your password is a very good one that is difficult to crack')
return Menu()
Notes to future readers:
Please do not write code in the form above:
Do not import modules you don't need
Do not use global variables
Do use loops: eg while True:
Do call functions from other functions
Do return values from one function back to the caller: optionchoice = menu()
The above code would have been better in this form:
import time
def menu():
print(" press 1 for password checker ")
print(" press 2 for password generator")
print(" press 3 to Quit")
return input("which option would you like to choose?")
def checkpasswordandinput():
while True:
optionchoice = menu()
if optionchoice == '1':
#Enter password and validate
pass
# detect other options
if optionchoice == '3':
break
checkpasswordandinput()
Those checks in the form UserPassword == UserPassword.isupper() will not work. Here, you are comparing a string, the password, to a boolean, the result of isX(). Hence, all those checks are False and you get to the else branch (if the length is acceptable).
In the case of upper and lowercase chars, you could use UserPassword == UserPassword.upper() instead (just upper, not isupper, and analogously with lower()), i.e. compare the password to it's upper/lowercased version, but for digits this does not work. Instead, you can use any to check if any char is a digit: any(c.isdigit() for c in UserPassword)
Edit: You can use UserPassword == UserPassword.upper() to check if the password does not contains any lowercase letters, which is kind of unintuitive. Instead, I'd suggest using any for all the checks and also inversing the condition so the "positive" case is in the ifs body and the "negative" in the else. Something like this:
up = UserPassword
if 8 <= len(up) <= 24 and any(c.isupper() for c in up) and any(c.islower() for c in up) and any(c.isdigit() for c in up) and any(c.isalpha() for c in up):
Or a bit shorter, using a list of functions:
if 8 <= len(up) <= 24 and all(any(f(c) for c in up) for f in (str.islower, str.isupper, str.isdigit, str.isalpha)):
So you need to do something like the following:
hasUpper = False
hasLower = False
hasDigit = False
etc etc
Then, go through the 'password' one character at a time:
ie. string[1], string[2], string[3]
Run your boolean result on it (isupper, islower, etc)
when true, change your hasUpper, hasLower, hasDigit to True
Then, once gone through the whole string, check your boolean results with your requirements. ie:
if requirements are upper, lower, and digit.
if hasUpper = True and hasLower = True and hasDigit = True:
then it's a good password, etc.
Make sense?
I have set up a program to change a "password". I have it checking to see if it is at least 8 characters, contains a capital letter and has a number, and if it does not meet this criteria, it asks for the password again. I have everything working except the checking for a number and I was wondering if someone could help.
npwv = 1
while npwv == 1:
npw = input("Please enter new password.")
npwc = input ("Please confirm new password")
if npwc == npw:
if npwc.isupper()== False:
if npwc.islower()== False:
if len(npwc) >= 8:
if str.isdigit(npwc) == True:
npw=npwc
print("Your password has been changed")
else:
print("Your password must contain a number")
npwv = 1
else:
print("Your password must contain at least 8 characters.")
npwv = 1
else:
print("Your password must contain at least 1 upper case character.")
npwv = 1
else:
print ("Passwords don't match")
npwv = 1
You are checking if the password itself is fully uppercase or composed of numbers. What you need to check if if the characters in the password match this criteria.
has_upper = any([c.isupper() for c in npwc])
has_digit = any([c.isdigit() for c in npwc])
You can also use regular expressions.
By the way, you should prefer getpass to get the password from the user.
Have you considered using .isalnum()?
>>> foo = "123asd"
>>> foo
'123asd'
>>> foo.isalnum()
True
>>>
Edit: Judging by the other answers, I am not sure what are you looking for, could explain it with examples?
I would suggest using sets, and the string package from stdlib for your list of acceptable characters.
I would also suggest refactoring a bit to remove a lot of the nesting with if / else branches.
import string
upper = set(list(string.uppercase))
lower = set(list(string.lowercase))
numbers = set(list(string.digits))
while True:
npw = input("Please enter new password: ")
npwc = input("Please confirm new password: ")
if npwc != npw:
print("Passwords don't match")
continue
if len(npcw) < 8:
print("Your password must contain at least 8 characters.")
continue
chars = set(list(npwc))
if not upper.intersection(chars):
print("Your password must contain at least 1 upper case character.")
continue
if not lower.intersection(chars):
print("Your password must contain at least 1 lower case character.")
continue
if not numbers.intersection(chars):
print("Your password must contain a number")
continue
npw = npwc
print("Your password has been changed")
break
This can be made more compact but yeah..
while True:
npw = input("Please enter new password.")
npwc = input ("Please confirm new password")
if npwc == npw:
if any(x.isupper() for x in npwc):
if any(x.islower() for x in npwc):
if len(npwc) >= 8:
if any (x.isdigit() for x in npwc):
npw=npwc
print("Your password has been changed")
#break # you probably want to exit the loop at this point
else:
print("Your password must contain a number")
else:
print("Your password must contain at least 8 characters.")
else:
print("Your password must contain at least 1 upper case character.")
else:
print("Your password must contain at least 1 lower case character.")
else:
print("Passwords don't match")
This looks like a job for regular expressions. Solution below:
import re
def password_validator(password):
if len(password) < 8:
return False, "Your password must contain at least 8 characters"
if not re.match('.*[0-9]', password):
return False, "Your password must contain a number"
if not re.match('.*[A-Z]', password):
return False, "Your password must contain at least 1 upper case character."
if not re.match('.*[a-z]', password):
return False, "Your password must contain at least 1 lower case character."
return True, "Your password has been changed"
while True:
npw = input("Please enter new password.")
npwc = input("Please confirm new password")
if npw != npwc:
print("Passwords don't match")
continue
is_valid, message = password_validator(npw)
print(message)
if is_valid:
break
You could also validate the whole thing in one go as:
pattern = """
(?=.*[a-z]) # use positive lookahead to see if at least one lower case letter exists
(?=.*[A-Z]) # use positive lookahead to see if at least one upper case letter exists
(?=.*\d) # use positive lookahead to see if at least one digit exists
[A-Za-z0-9##$%^&+=]{8,} # match any character in [] at least 8 times
"""
pwd_validator = re.compile(pattern, re.VERBOSE)
if pwd_validator.match(password):
# legit password
else:
# no match ask again
Hope this helps. The re.VERBOSE just makes this regular expression self-documenting so a lot easier to understand in future.
Very limited on using python and totally stuck, I've managed to get a while loop running on the code below so that the user can keep entering a code until they put in the correct one.
What I'm now looking to do is add a for loop so that it only asks the user to enter the code (4 wrong digits) 3 times and then locks them out. At the same time it needs a while loop to ensure if the user puts more than or less than 4 digits it continually runs and doesn't lock them out.
I just cant get the for loop and while loop working at the same time and don't know what I'm doing wrong.
user = ("1234")
valid = False
while not valid:
#for i in range (3):
user = input("Hello, welcome! Please enter a four digit passcode to open the safe: ")
user = user.upper()
if user == ("1234") :
print("You have cracked the code, well done")
valid = True
break
if user != ("1234") :
print ("that is incorrect, please try again")
valid = False
elif len(user) > 4:
print ("That is incorrect, please try again")
valid = False
elif len(user) < 4:
print ("That is incorrect, please try again")
valid = False
else:
print ("You have been locked out!! Alarm!!!!!!")
user = ("1234")
counter = 0
while counter < 3:
user = input("Hello, welcome! Please enter a four digit passcode to open the safe: ")
user = user.upper()
if len(user) != 4:
print("I said 4 digits!")
continue
if user == ("1234") :
print("You have cracked the code, well done")
break
print ("that is incorrect, please try again")
counter += 1
if counter == 3:
print ("You have been locked out!! Alarm!!!!!!")
else:
print ("Everything is fine.")
I adapted your code a little bit and added a counter variable
user = ("1234")
valid = False
counter = 0
while not valid:
user = input("Hello, welcome! Please enter a four digit passcode to open the safe: ")
user = user.upper()
if user == ("1234") :
print("You have cracked the code, well done")
valid = True
break
elif len(user) != 4:
print ("That is incorrect, please try again")
else:
print ("that is incorrect, please try again")
counter = counter + 1
if counter == 3:
print ("You have been locked out!! Alarm!!!!!!")
#Do stuff to actually abort here...
It counts up if there is a wrong answer now
the inner loop is just for valid input:
user = '1234'
locked = False
miss_cnt = 0
while True:
while True:
ans = raw_input('User -> ')
if len(ans) == 4 and ans.isdigit():
break
if ans != user:
miss_cnt += 1
if miss_cnt >= 3:
locked = True
break
else:
break
I left out the prints for clarity of flow
The following code should work for you.
answer = "1234"
valid = False
count = 0
while not valid and count < 3:
user = input("Hello, welcome! Please enter a four digit passcode to open the safe: ")
user = user.upper()
if user == answer:
print("You have cracked the code, well done")
valid = True
elif count < 2:
print ("that is incorrect, please try again")
else:
print ("You have been locked out")
count += 1
I took the () off of the strings because that would make them sets so the if statement would never be true, because sets don't equal string inputs.
I'm making a 4 digit password guesser in python 3. I want to make sure that you can only put in 4 digit passwords and not 5 or 6 digit passwords. Here is the code I have so far.
print('your password will not be used or saved for later use you do not have to put in your real password')
real_password = int(input("please enter your four digit phone password here:"))
computer_guess = 0000
guess_counter = 0
yes = 'yes'
no = 'no'
print ("guessing your password...")
while computer_guess < real_password:
computer_guess = computer_guess + 1
guess_counter = guess_counter + 1
print("guess number", guess_counter)
print ("your password is", computer_guess)
Before you cast the input to an int, cast it to a str instead, then you can call the len() builtin method to check the length of the entered string. Check the documentation for details on this method. If it is greater than 4, then you should recall your input call. Something like the following should work:
>>> real_password = input("please enter your four digit phone password here: ")
please enter your four digit phone password here: 1234
>>> while len(str(real_password)) != 4:
... real_password = input("please enter your four digit phone password here: ")
In this condition the loop would not be ran, however if the entered string was not equal to 4, the loop would run until that condition was satisfied.
print('your password will not be used or saved for later use you do not have to put in your real password')
def get_password():
real_password = int(input("please enter your four digit phone password here:"))
if len(str(real_password)) != 4: # condition is not met if your variable
get_password() # is not 4, easily changed if you
else: # want
return real_password
#define a method and ask it to call itself until a condition is met
#
real_password = get_password() # here the method is called and the return
computer_guess = 0 # value is saved as 'real_password'
guess_counter = 0
yes = 'yes' # these are not used in your code
no = 'no' # but I'm am sure you knew that
print ("guessing your password...")
while computer_guess != real_password: # your loop should break when the
computer_guess += 1 # is found, not some other implied
guess_counter += 1 # the '+=' operator is handy
print("guess number", guess_counter)
print ("your password is", str(computer_guess)) # explicitly define the int
# as a string
I hope that helped...
These are my two def functions:
def valid_username(username):
# implement the function here
while True:
try:
username = input("Username: ")
if len(username) < 8:
print ("Sorry, the username must be at least 8 characters long.")
if username.isalnum() == False:
print ("Sorry, your name can only contain alpha numeric characters")
numupper = 0
for c in username:
if c.isupper() == True:
numupper += 1
if numupper > 0:
print ("You have at least 1 uppercase in this username.")
else:
print ("You have no uppercase in this username.")
numlower = 0
for d in username:
if d.islower() == True:
numlower +=1
if numlower > 0:
print ("You have at least 1 lowercase in this username.")
else:
print ("You have no lowercase in this username.")
numdigit = 0
for e in username:
if e.isdigit() == True:
numdigit += 1
if numdigit > 0:
print ("You have at least one digit in this username.")
else:
print("You have no digits in this username.")
continue
except:
print ("Sorry, not valid. Try again.")
else:
print ("Thank you for your input")
break
def valid_password(password, username):
# implement the function here
while True:
try:
password = input("Password: ")
if username in password:
print ("That's not secure at all.")
if len(password) < 8:
print ("Sorry, the password must be at least 8 characters long.")
if password.isalnum() == False:
print ("Sorry, your password can only contain alpha numeric characters")
numupper = 0
for c in password:
if c.isupper() == True:
numupper += 1
if numupper > 0:
print ("You have at least 1 uppercase in this password.")
else:
print ("You have no uppercase in this password.")
numlower = 0
for d in password:
if d.islower() == True:
numlower +=1
if numlower > 0:
print ("You have at least 1 lowercase in this password.")
else:
print ("You have no lowercase in this password.")
numdigit = 0
for e in password:
if e.isdigit() == True:
numdigit += 1
if numdigit > 0:
print ("You have at least one digit in this password.")
else:
print("You have no digits in this password.")
continue
except:
print ("Sorry, not valid. Try again.")
else:
print ("Thank you for your input")
break
my main program is this:
username = input("Username: ")
result, reason = uservalidation.valid_username(username)
if not(result):
print (reason)
else:
password = input("Password: ")
pwresult, pwreason = uservalidation.valid_password(password, username)
if not(pwresult):
print (pwreason)
else:
print ("Username and Password combination is valid!")
when I run it I get the following:
Username: d
Username: user
Sorry, the username must be at least 8 characters long.
You have no uppercase in this username.
You have at least 1 lowercase in this username.
You have no digits in this username.
Username: craig2345
You have no uppercase in this username.
You have at least 1 lowercase in this username.
You have at least one digit in this username.
Thank you for your input
Traceback (most recent call last):
File "C:/Users/Si Hong/Desktop/pythontest/HuangSiHong_Assign8_part3.py", line 7, in <module>
result, reason = uservalidation.valid_username(username)
TypeError: 'NoneType' object is not iterable
>>>
I'm having trouble figuring out why when I type enter in the first value for username, it doesn't trigger the function, but does it after the 2nd time I enter it in, and also I am not sure how to solve the nonetype error issue, if someone can explain this to me, that would be great thank you so much!
If a function doesn't have a value explicitly returned then it implicitly returns None, which cannot be applied to tuple unpacking this way.
The reason that you're seeing the Username: prompt twice is that you ask for input once in the main program and then again, immediately, inside the function. The function should not ask the user for any input, it gets the value of username from the argument passed to it.
The reason you're seeing the error about NoneType (not nonetype as you wrote, Python is case-sensitive and you should really get in the habit of typing exactly what it says) is that your function valid_password() does finish with a return statement returning two values as expected by your program. In fact, it doesn't have a return statement at all, which means it effectively returns the special Python value None (which is an object of type NoneType).
Because you tell your program to extract two values from whatever the function returns, it's trying to do so by iterating over the values returned. NoneType is not one of the types that can be iterated over (since it contains no information), thus the error message.