So in my computer programming class we've been tasked with making the generic password strength checker, mine works and checks the length correctly but for whatever reason the 2nd part doesn't work properly and I can't figure out why.
password = input("Please enter a password between 6 - 12 characters long: ")
while len(password) in range(6, 13) == False:
if len(password) < 6:
print("Password too low!")
password = input("Please enter a password between 6 - 12 characters long: ")
elif len(password) > 12:
print("Password too high!")
password = input("Please enter a password between 6 - 12 characters long: ")
Upper = 0
Lower = 0
Numerical = 0
for char in password:
if char == char.isupper():
Upper = Upper + 1
elif char == char.islower():
Lower = Lower + 1
elif char == char.isnumeric():
Numerical = Numerical + 1
Strength = int(Upper + Lower + Numerical)
if Strength == 1:
print("Password is weak!")
elif Strength == 2:
print("Password is medium!")
elif Strength == 3:
print("Password is strong!")
Any help would be greatly appreciated, I browsed other similar problems but with something specific it's kind of difficult.
EDIT: Removing the parenthesis from my while loop will do nothing, it works the same anyway. The in range part makes no difference, I still get the error:
Traceback (most recent call last):
File "C:\Users\Owen\Desktop\Password Checker.py", line 21, in
for char in password():
There are a lot of little issues you can fix with this section of code so I can't really tell you what is producing the improper output.
1) as Ken stated:
len(password) != range(6, 12) will always return true. This is because you are comparing an int to a list.
range(6, 12) gives you [6, 7, 8, 9, 10, 11] so the length of the password can never be equal to that statement. However, if you use len(password) in range(6, 12) the length of the password is compared to each value in the list until there is a match (if there is one). One more thing, using range(a, b) only includes numbers from a to b-1. So you really want:len(password) in range(6, 13)
2) The logic of the while loop should change with changes from my first point.
If len(password) in range(6, 13) evaluates to True, then the while statement won't loop and you will move on to the next section. You can delete the else section and change your elif to else.
3) You can get rid of LengthLow and LengthHigh since you aren't using them anywhere.
4) password is a string so you can't call it with the () operator. Your for loop should be for char in password:
5) Use char.isupper(), char.islower(), or char.isnumeric() to check if they are numeric values
6) You don't need to store Upper, Lower, and Numerical separately since you are just adding them together at the end anyways. You can actually accomplish this with a one liner if you are familiar with list comprehensions
strength = sum([1 if c.isupper() or c.islower() or c.isnumeric else 0 for c in password])
Although, it seems to me that this is all the same as c.isalnum() for alphanumeric. So write,
strength = sum([1 if c.isalnum() else 0 for c in password])
7) I don't know anything about password strength calculation, but I think special characters should be considered as well. In that case c.isalnum() won't be the magic all in one that it is in your current code.
There may be more bugs that I haven't noticed, but that should definitely get you on your way to fixing the problem.
ero1ca's answer explains many of the issues.
As for the last part of the code not working, that is because you loop through every char in the password, and increment based on what that character is. With a mandated length > 3, your conditional statements at the end wont cover the password!
below is a revised version of the code.
password = input("Please enter a password between 6 - 12 characters long: ")
while not len(password) in range(6, 13):
if len(password) < 6:
print ("Password too low!")
else:
print ("Password to high!")
password = input("Please enter a password between 6 - 12 characters long: ")
Upper = 0
Lower = 0
Numerical = 0
if any(x.isupper() for x in password):
Upper = 1
if any(x.islower() for x in password):
Lower = 1
if any(x.isdigit() for x in password):
Numerical = 1
Strength =int(Upper + Lower + Numerical)
if Strength == 1:
print ("Password is weak!")
elif Strength == 2:
print ("Password is medium!")
else:
print ("Password is strong!")
Use raw_input instead of input() for python 2.7
Edit Holy Necro! My mistake lads!
Related
I'm trying to write code that can check if an input contains;
At least 8 letters, whereas at least 1 of those is a number (0-9)
Contains an upper and lower case character
I keep getting stuck in a "inputs password, returns true, and input password again, exit" single loop..
Fairly new at programming, doing my first semester atm so all help would be appreciated!
This is my program so far
def is_valid():
valid = 0
password = input("Password: ")
for ele in password:
if ele.isupper and ele.islower and ele.isdigit and len(password) > 7:
return "True"
else:
return "False"
print(is_valid())
is_valid()
I tried moving the print inside the function, as I think it is intended, by then It won't print..
for ele in password:
if ele.isupper and ele.islower and ele.isdigit and len(password) > 7:
return "True"
else:
return "False"
This code has several problems.
First, you're referring to the ele.isupper function, but because you don't have parentheses (), you're not calling the function. So the code is basically asking "Does ele.isupper exist"? Well yes, it is a function, and it exists, so the if condition is true.
Use ele.isupper() instead of ele.isupper.
Second, even if you fix that problem (and also the same problem for ele.islower and ele.isdigit), there's no way that one letter will be uppercase AND lowercase AND a digit.
Third, the return statement makes the function exit immediately, so your loop will only look at the first letter. Instead of doing that, you want to loop over all the letters, and move the return statement to after the loop.
I think you were actually looking for code like this:
uc = 0
lc = 0
digits = 0
for ele in password:
if ele.isupper():
uc += 1
elif ele.islower():
lc += 1
elif ele.isdigit():
digits += 1
if uc > 1 and lc > 1 and digits > 1 and len(password) > 7:
return "True"
else:
return "False"
There are many ways to do this. But first I will clarify some mistakes.
First, when you write ele.isupper you are not calling the function, for that you must put the parenthesis: ele.isupper().
Secondly, your loop is looping through each letter of the password and in the case of solving the previous bug you would find that the condition would never be fulfilled since a character cannot be all things at the same time.
I leave you an example of how you can solve these problems, as I said, there are many ways to do it but I present you one that is not complex and uses the basics. Also, if the password is incorrect, ask for it again on the screen.
def is_valid():
valid = [0, 0, 0]
password = input("Password: ")
for ele in password:
if len(password) < 8:
break
elif ele.isupper() and valid[0] == 0:
valid[0] = 1
elif ele.islower() and valid[1] == 0:
valid[1] = 1
elif ele.isdigit() and valid[2] == 0:
valid[2] = 1
if sum(valid) < 3:
print('False')
is_valid()
else:
print('True')
is_valid()
Output:
Password: hello
False
Password: Hello
False
Password: Hello World
False
Password: Hello World 1
True
The code first checks if the length is correct, if it is not, it does not make any further checks.
If this condition is met it continues and as conditions are met, one of the variables in the valid list is incremented. You can do this with a number instead of a list, but if you want to specify what has gone wrong you can access each value in the list to check it or say that n conditions have failed, for example:
if valid[1] == 0:
print('There must be at least one lower character').
There are a couple of problems in your code.
Reaching return statement exits the loop and the function itself, returning the value. So you are just checking first letter and the immediately finish the loop.
valid = 0 seems to be never used.
Last instruction if your function print(is_valid()) would also have no effect.
One possible straightforward solution to your problem would be to set a number of different flags for things you want to check, ant then once you find them, set te proper value for the flags. Like this:
def is_valid():
password = input("Password: ")
has_upper = False
has_digit = False
has_lower = False
has_good_length = False
for ele in password:
if ele.isupper():
has_upper = True
if ele.islower():
has_lower = True
if ele.isdigit():
has_digit = True
if len(password) > 7:
has_good_length = True
if has_upper and has_lower and has_digit and has_good_length:
return True
return False
print(is_valid())
You should also check for characters that are neither letters nor digits (according to the rules stated in the question) - e.g., punctuation.
How about using a bit mask?
def is_valid(pwd):
state = 0
if len(pwd) >= 8:
for c in pwd:
if c.isdigit():
state |= 1
elif c.islower():
state |= 2
elif c.isupper():
state |= 4
else:
state |= 8
return state == 7
password = input('Password: ')
print(is_valid(password))
You're going element-wise in a loop on your password, rather than considering the whole thing together. You want something like:
def is_valid():
password = input("Password: ")
if not any(el.isupper() for el in password):
return False
if not any(el.islower() for el in password):
return False
if not any(el.isdigit() for el in password):
return False
if len(password) < 8:
return False
return True
is_valid()
for ele in password will iterate through the characters in the user's input.
your if statement doesnt make sense. ele.isupper and ele.islower will never be true at the same time.
if statement needs work. make booleans for each condition you want to validate and set them to true individually is you see the required elements.
My program is trying to create a program that identifies a program that includes 3 functions: 1 to identify the length, 1 to make sure they're are not any spaces and 1 that makes sure that the program checks that you're using at least 1 number and letter. I basically have almost everything that I want, I'm just not understanding how to put them together and work cohesively.
In the passlength function, I get the correct outcome if I were to add "password = input("Enter password: ")" at the beginning of the line. However, I'm trying to do that within the main function. Then it should collapse in that function to check whether or not you have the correct amount of characters. No less than 6 characters, but no more than 15.
In the next function, I'm trying to make sure no spaces are in the password, however no matter what I do it keeps rendering false.
I've tried the same thing as above with the "isalpha" and "isdigit" functions as well. However, I'm unsure how it would check from 0 to 15 make sure that at least 1 digit and 1 alphabetical letter are used.
But my overall concern is how to use a variable in my main function then transfer that don't do my subfunctions for results, then using an if statement loop it back in case your password isn't safe.
def passLength():
password = input("Enter password: ")
if len(password) > 5 and len(password) < 15:
print("Password Length - Confirmed")
else:
print("Password length - Invalid, please try again")
passLength()
def passSpace():
password = input("Enter password: ")
passwordSpace = password.isspace()
print(passwordSpace)
def main():
password = input("Enter password: ")
passLength()
Here is the kind of organization you need. The validation functions just do validation. It is up to the caller to decide what to do with the information.
def passLength(password):
return 5 < len(password) < 15
def passSpace(password):
return ' ' not in password
def main():
while True:
password = input("Enter password: ")
if not passLength(password):
print("Too short or too long. Try again.")
elif not passSpace(password):
print("No spaces allowed. Try again.")
else:
print( "Success" )
break
main()
I need to write a pseudocode for the random password generator below. How do I write a pseudocode Do help me out. Thank you.
import random
import string
print('hello, Welcome to Password generator!')
l = False
while not l:
length = int(input('\nEnter the length of password: '))
if length < 8 :
print('You password length is too short(must be more than 8 character)')
print(length, "is the length of your password")
elif length > 16:
print('You password length is too long(must be less than 17 character)')
print(length, "is the length of your password")
else:
print('You password length looks good')
break
lower = string.ascii_lowercase
upper = string.ascii_uppercase
num = string.digits
symbols = string.punctuation
all = lower + upper + num + symbols
temp = random.sample(all,length)
password = "".join(temp)
print(password)
is this good enough?
IMPORT random
IMPORT string
OUTPUT('hello, Welcome to Password generator!')
SET l TO False
WHILE not l:
SET length TO int(INPUT('\nEnter the length of password: '))
IF length < 8 :
OUTPUT('You password length is too short(must be more than 8 character)')
OUTPUT(length, "is the length of your password")
ELSEIF length > 16:
OUTPUT('You password length is too long(must be less than 17 character)')
OUTPUT(length, "is the length of your password")
ELSE:
OUTPUT('You password length looks good')
break
SET lower TO string.ascii_lowercase
SET upper TO string.ascii_uppercase
SET num TO string.digits
SET symbols TO string.punctuation
SET all TO lower + upper + num + symbols
SET temp TO random.sample(all,length)
SET password TO "".join(temp)
OUTPUT(password)
or this one :
DECLARE length<-int(input('\nEnterthelengthofpassword:')) : INTEGER
import random
import string
OUTPUT 'hello, Welcome to Password generator!'
l<-False
WHILE not l DO
INPUT length<-int(input('\nEnterthelengthofpassword:'))
IF length < 8
THEN
OUTPUT 'You password length is too short(must be more than 8 character'
OUTPUT length, "is the length of your password"
ENDIF
elif length > 16: //You will need to change this to CASE OF
OUTPUT 'You password length is too long(must be less than 17 character'
OUTPUT length, "is the length of your password"
ELSE
OUTPUT 'You password length looks good'
break //This might be better as a repeat loop
ENDWHILE
lower<-string.ascii_lowercase
upper<-string.ascii_uppercase
num<-string.digits
symbols<-string.punctuation
all<-lower + upper + num + symbols
temp<-random.sample(all,length)
password<-"".join(temp)
OUTPUT password
Process finished with exit code 0
Pseudocode is kind of code, and is meant to show the steps of a specific algorithm. For this algorithm, it might look like this.
output "Welcome"
l := false
while l is not true do
length := int value of input("Enter length of password: ")
if length < 8 do
output "Your password is too short"
else if length > 16 do
output "Your password is too long"
else do
output "Password length looks good"
break from loop
endif
endwhile
lower := array of lowercase letters
upper := array of uppercase letters
numbers := array of digit characters
symbols := array of punctuation characters
all := lower combined with upper combined with numbers combined with symbols
temp := random sample of all, with length len
password := join "" with temp
output password
For more reading on pseudocode, look at this link: https://en.wikipedia.org/wiki/Pseudocode
Hi everyone I’m writing because I’m stuck with an exercise in which I should only use for loops and if/else statements. I found a way but practically I’m iterating the same block of code four times and I’m really looking for a way to automate it.
I know that probably this is not the best way to solve the exercise but now I’m not looking for the most efficient way (I already found on the solutions of the exercise), I’m asking you how can I use for to iterate the block of code
The exercise tells me to create a program that takes an IP address from the keyboard and validates that it can be interpreted as a valid IP address.
An IP address consists of 4 numbers, separated from each other with a full stop. Each number can have no more than 3 digits. (Examples: 127.0.0.1)
Important
This challenge is intended to practise for loops, and if/else statements, so although it would probably be written for real using regular expressions we don't want you to use them here even if you know what they are.
This is what I made:
# ipAddress = input("please enter an ipAddress: ")
ipAddress = "192.168.7.7" #test ip address
# check if number of dots is 3
numberOfDot = 0
for char in ipAddress:
if char == '.':
numberOfDot += 1
totNumbOfDot = numberOfDot # output of this section is totNumberOfDot, to be checked at the end
if totNumbOfDot != 3:
print("You inserted a wrong ip address")
# first number check # THIS IS THE BLOCK OF CODE I WANT TO
number1 = '' # ITERATE WITH FOR IF POSSIBLE
for char in ipAddress:
if char in "0123456789":
number1 += char
if char == '.':
break
if 1 <= len(number1) <= 3:
print("First number: OK")
else:
print("First number: Fail")
digitN1 = len(number1) + 1
print(number1)
# second number check
numberMinus2 = ipAddress[digitN1:]
number2 = ''
for char in numberMinus2:
if char in "0123456789":
number2 += char
if char == '.':
break
if 1 <= len(number2) <= 3:
print("Second number: OK")
else:
print("Second number: Fail")
digitN2 = len(number2) + digitN1 +1
print(number2)
# third number check
numberMinus3 = ipAddress[digitN2:]
number3 = ''
for char in numberMinus3:
if char in "0123456789":
number3 += char
if char == '.':
break
if 1 <= len(number3) <= 3:
print("Third number: OK")
else:
print("Third number: Fail")
digitN3 = len(number3) + digitN2 + 1
print(number3)
# fourth number check
numberMinus4 = ipAddress[digitN3:]
number4 = ''
for char in numberMinus4:
if char in "0123456789":
number4 += char
if char == '.':
break
if 0 < len(number4) <= 3:
print("Fourth number: OK")
else:
print("Fourth number: Fail")
digitN4 = len(number4) + digitN3 + 1
print(number4)
I would also say, split() is the way to go. Your question was if there was a way to use your logic and still not have to repeat code 4 times. To achieve that you could do something like this:
numberOfDot=0
number=""
for char in ip+'.':
if char=='.':
numberOfDot+=1
if len(number) in [1,2,3]:
print("number %d OK"%numberOfDot)
else:
print("number %d not OK"%numberOfDot)
print(number)
number=""
elif char in '1234567890':
number+=char
else:
print("character not valid")
if numberOfDot!=4:
print("you inserted a wrong ip")
As i said, i would also recommend using split() - this is just to try and provide an answer closer to your question. Also please note that this code (same as yours) will mark ip adresses containing letters, not only numbers, as OK.
Well, you have to ask yourself the right question: "can I do better?". Please always do that. Yes, in fact, you can. The code that deals with numbers validation between dots is essentially the same. So you should split the string on dots and use for loop to validate each group:
for str in ipAddress.split("."):
your validation here
how about that? split the string at the dots . and check if between the dots there are numbers in the valid range (that would also accept '255.255.255.255')
def valid(ipaddress):
# we need 3 dots; otherwise this is no ipaddress.
if not ipaddress.count('.') == 3:
return False
# check what is before, between and after the dots
for byte in ipaddress.split('.'):
# if byte can not be interpreted as an integer -> no good!
try:
i = int(byte)
except ValueError:
return False
# now check if byte is in the valid range
if i < 0:
return False
if i > 255:
return False
return True
print(valid(ipaddress='192.168.7.7')) # -> True
print(valid(ipaddress='.168.7.7')) # -> False
print(valid(ipaddress='721.168.7.7')) # -> False
This question already has answers here:
How to test if a string has capital letters
(3 answers)
Closed 2 years ago.
I was supposed to write a program that verifies a username. The username must consist of at least 8-15 letters, no alphanumeric values. So, you can only have numbers and letters. You can't have numbers at the beginning or the end. And, You MUST have at least one capital and lower case letter and at least one number. I got how to do everything but how to get the program to check to see if it contains any capital letters. I tried to do the " if not in " but no luck. This is what I have so far.
username = input("please enter a name: ")
for i in username:
while len(username) < 8 or len(username) > 15:
print("Password is too long or too short")
username = input("please enter a name: ")
j = 31
while j < 47:
j += 1
while chr(j) in i:
print("No alphanumeric values allowed.")
username = input("please enter a name: ")
k = 57
while k < 64:
k += 1
while chr(k) in i:
print("No alphanumeric values allowed.")
username = input("please enter a name: ")
l = 47
while l < 57:
l += 1
while chr(l) in username[0]:
print("you cannot have a number in the beggining")
username = input("please enter a name: ")
while chr(l) in username[-1]:
print("you cannot have a number in the end")
username = input("please enter a name: ")
You can use any with a generator to test if the string has a capital letter
testString = "abjKcf"
print(any(x.isupper() for x in testString)) #true
Good Solution
As for addressing a solution to your problem, welcome to the world of generator expressions and assertions
while True:
testString = input()
try:
assert 8 <= len(testString) <= 15, "String must be between 8 and 15 characters"
assert all(x.isalnum() for x in testString), "String must be alphanumeric"
assert any(x.isupper() for x in testString), "String must contain one capital"
assert any(x.islower() for x in testString), "String must contain one lowercase"
assert any(x.isdigit() for x in testString), "String must contain one digit"
assert testString[0].isdigit() == False, "No numbers at start"
assert testString[-1].isdigit() == False, "No numbers at end"
break #if we haven't hit any errors then the username fits the criterion
except Exception as e:
print(e)
Really ugly solution (as requested)
Basically you set up some booleans and try to disprove them by looping through every character and checking if it meets some conditions
while True:
testString = input()
allAlphaNumeric = True
oneCapital = False
oneLowerCase = False
oneDigit = False
for letter in testString:
if not letter.isalnum():
oneAlphaNumeric = False
if letter.isupper():
oneCapital = True
if letter.islower():
oneLowerCase = True
if letter.isdigit():
oneDigit = True
numberAtStart = testString[0].isdigit()
numberAtEnd = testString[-1].isdigit()
if allAlphaNumeric and oneCapital and oneLowerCase and oneDigit and not numberAtEnd and not numberAtStart:
break
if not 8 <= len(testString) <= 15:
print("String must be between 8 and 15 characters")
if not allAlphaNumeric:
print("Your string must be alphanumeric!")
if not oneCapital:
print("Your string must contain at least one capital letter")
if not oneLowerCase:
print("Your string must contain atleast one lowercase letter")
if not oneDigit:
print("Your string must contain atleast one digit")
if numberAtStart:
print("You cannot have a number at the start")
if numberAtEnd:
print("You cannot have a number at the end")