isupper() and isdigit() not working together - python

I am trying to create a simple passwprd application and I want to check if my password meets some requierments. I want it to have both an uppercase letter and a digit somewhere. I am storing each letter of the password in a list using a for loop and I then check if each element in the list is uppercase. But when I, in the if statement that checks if the letter is uppercase, add a new if statemnt checking if one of the characters is a digit the if statement doesn’t return anything.
def check_password():
characters = []
if len(password_entry.get()) >= 8:
for i in range (len(password_entry.get())):
characters.append(password_entry.get()[i])
if characters[i].isupper():
if characters[i].isdigit():
register_user()
print("password valid")
else:
password_not_valid_upper()
else:
password_not_valid_length()

You're checking that the character is uppercase, and if it is you're then checking if it's a digit. It can't be both so you're always falling through to the else password_not_valid_upper().

Related

Why is my program not detecting uppercase characters?

I am asking the user to input a password. The program then determines if the password is valid if it meets certain criteria.
Here is the section in question.
for i in range(0, len(password)):
if(password[i].isdigit()==True):
isNum+=1
elif (password[i].isalpha()==True):
isLetter+=1
elif (password[i].isupper()==True):
isUpper+=1
My program detects numbers and letters just fine, but it doesn't detect uppercase letters. If I put in the password 1234Foxes, it will say that there are 4 letters, 4 numbers, and 8 total characters, but it states that there are 0 uppercase letters.
If isupper is true, isalpha must have been true since an uppercase letter is necessarily alphabetic. Since you're using elif, the conditions are exclusive, and the checks stop once the isalpha check is true.
Just don't use elif there if you want both checks to run:
for character in password:
if(character.isdigit()):
isNum += 1
elif (character.isalpha()):
isLetter += 1
if (character.isupper()):
isUpper += 1
I also got rid of the need to index password by iterating the String directly, and the redundant == True
your logic is correct, but, you need to check if a char is upper before if it is alpha. It's because every upper char is alpha, so, the last elif will never be reached. So, change your code position to this:
for i in range(0, len(password)):
if(password[i].isdigit()==True):
isNum+=1
# isupper() first from isalpha()
elif (password[i].isupper()==True):
isUpper+=1
elif (password[i].isalpha()==True):
isLetter+=1
And, another tip: you can go through every char of string without using indexing, like this:
for char in password:
if (char.isdigit()):
isNum += 1
elif (char.isupper()):
isUpper += 1
elif (char.isalpha()):
isLetter += 1
Since isdigit(), isalpha() and isupper() returns True or False, you don't need to check if it is True or False (no need the == operator), just put it into if statement.

Making a valid password checker. Can not get my program to get through the if conditions to print a valid password

I have been assigned the following exercise as homework:
Some Web sites impose certain rules for passwords. Write a function that checks whether a string is a valid password. Suppose the password rules are as follows:
A password must have at least eight characters.
A password must consist of only letters and digits.
A password must contain at least two digits.
Write a program that prompts the user to enter a password and displays valid password if the rules are followed or invalid password otherwise.
I know there's a more efficient and proper way of doing this but i'm just starting out so I don't necessarily need to be those right now. Just want to finish this question.
The counter/ accumulator works and I don't get any errors but I can not fulfill the if condition correctly so that this program prints "valid password"
password = str(input("Enter in a password to be checked: "))
def valid_password_checker(password):
from string import ascii_lowercase as alphabet
digits = '0123456789' # creates a string of digits
digit = 0 # acc for digits
length = 0 # acc for length
for char in password: # calls on each character in string
if char in alphabet:
length += 1
if char in digits:
digit += 1
if digit >= 2:
flag = True
if length >= 8 and digit is True:
print("valid password")
else:
print("Password does not contain enough characters or digits.")
else:
print("Password does not contain enough digits.")
valid_password_checker(password)
The problem with your existing code is that the variable digit is a number, and therefore doing digit is True as you have done in your if statement, always return False. If you remove digit is True, then your existing solution will work. Take a look however at my version:
def valid(password):
digits = 0
characters = 0
for char in password:
if char.isalpha():
characters += 1
elif char.isdigit():
digits += 1
characters += 1
if characters >= 8:
if digits >= 2:
print("Password is valid")
else:
print("Password doesn't contain enough digits")
else:
print("Password doesn't contain enough characters")
I have made the following modifications from your original:
Used the built-in function str.isdigit() for checking if a character is a digit.
Used the built-in function str.isalpha() for checking if a character is a letter of the alphabet
Moved everything but the counting operations outside of the for loop, so that the function doesn't print multiple things
If you want, you can undo the first two changes, if you're worried about your teacher knowing you asked for help. However, I wouldn't turn in the solution that prints "Password doesn't contain enough digits" as many times as there are characters in the inputted password.
you can write something like this:
password = str(input("What is the password that you want to validate: "))
def get_digits(password):
return [i for i in password if i.isdigit()]
numbers = ''.join(get_digits(password))
if (len(password) < 8) or (len(numbers) < 2):
print(password, "is an invalid password")
else:
print(password, "is a valid password")
nice and simple.

Python - hangman replacing letters and updating with count

def update(dictionary,letter):
if letter in dictionary["word"]:
I am making hangman game and I am stuck on how I can make my last function so that when a letter is guess
if letter in dictionary['word'] and letter not in dictionary['guessWord']:
dictionary['guessWord'] = ''.join([i if i in dictionary['guessWord'] or i == letter
else '*' for i in dictionary['word']])
else:
dictionary['lives'] -= 1
This techniques rebuilds guessWord from the secret word every time. join takes a list of strings and joins them together with a separator, in this case ''. For each character in the secret word, we examine whether that character is equal to letter or is already in the guessword. If so, it is put in the guessword. If not, it is replaced with *
You could use zip to iterate over tuples (character in word, character in guess). If letter given to update matches with character in correct answer then pick it, otherwise pick the current guess and join the picked characters together. Finally check if new guess same as current and in case they match reduce lives:
def update(dictionary, letter):
old = dictionary['guessWord']
dictionary['guessWord'] = ''.join(x if x == letter else y
for x, y in zip(dictionary['word'], old))
if old == dictionary['guessWord']:
dictionary['lives'] -= 1

How to check if a word is a palindrome (Python)

Please help...
So the instruction says to program the computer to check whether a word is a palindrome or not. I inputted this code:
def is_palindrome(word):
counter_from_first_letter=0
counter_from_last_letter=-1
from_first_letter = word[counter_from_first_letter]
from_last_letter = word[counter_from_last_letter]
max_index_from_first= len(word)
max_index_from_last= (len(word))*-1
while from_first_letter == from_last_letter:
from_first_letter = word[counter_from_first_letter]
from_last_letter = word[counter_from_last_letter]
counter_from_first_letter += 1
counter_from_last_letter -= 1
return True
The problem is the computer only checks whether the first and last letters are the same, and if they are, it just returns true. How do I make sure the computer checks every single letter? Thanks
Maybe something like this:
def is_palindrome(word):
if word == word[::-1]:
return True
else:
return False
in python-3
name = 'madam'
print(name.find(name[::-1]) == 0)
Maybe you can try this: first convert your string into a list, then reverse the list and convert it back into a string. compare both the strings and if they match? they are palindromes, if not, they aren't.
'''checking whether a word is a palindrome
we first convert the word into a list then join it;
use an if statement to compare the two strings'''
def palindrome(string):#you need the input(string)
palindrome_check=[]#create an empty list
for character in string [::-1]:#create a list from the input
#(use a for loop because you now know the range)
#the [::-1] analyzes the characters in reverse
palindrome_check.append(character)#add each character to the new empty list
#print(palindrome_check)
rev_string= ''.join(palindrome_check)#.join -creates a string from the created list
print(rev_string)
#REMOVE SPECIAL CHARACTERS- IM THINKING OF A LOOPING THROUGH, BUT NOT SURE HOW TO IMPLEMENT IT
string=string.replace(' ', '')
rev_string=rev_string.replace(' ', '')
string=string.replace(',', '')
rev_string=rev_string.replace(',', '')
string=string.replace('.', '')
rev_string=rev_string.replace('.', '')
#THIS IS THE LOGIC: IT CHECKS BOTH STRINGS, if they are equal, it is a palindrome;
if string.lower()==rev_string.lower():
return True, print('It is a Palindrome')
else:
return False, print('It isnt a palindrome')
#call the function; key in the parameters-
palindrome= palindrome("No, Mel Gibson Is A Casinos Big Lemon")
#maybe we can try having a user key in the parameter? lets try
#palindrome=palindrome(input('kindly enter your word/phrase '))-wrong
#print('Kindly enter your word or phrase')
#user_palindrome=input('')
#palindrome=palindrome(user_palindrome)
#it wont work this way either
If you can have the user define the parameter(string), the better, if you know how to do this, kindly share.
To check whether a word or phrase is a palindrome, it be necessary to check if the original sentence is equal to the original sentence reversed.
word = "Eva can I see bees in a cave"
word_lower = word.lower().replace(" ", "")
if word_lower == word_lower[::-1]:
print("It's a palindrome")
else:
print("This is not a palindrome")

Having trouble learning how to look at individual characters in a string checking for case or digit

I am trying to write a simple password checker for homework assignment, looking for at least one capital, one lower case, one digit, and it needs to be 6 or more chars.
I have searched and searched on here and elsewhere, but either what I read doesn't match our instruction, or the replies are more advanced than myself. Any help I get will be cited as a comment in my assignment.
This is just the part of my code which checks for caps, it only looks at the whole string, not the individual characters and I can't seem to find the solution.
passwd = input('enter password: ') ## we are actually using (sys.agrv)
## but I am using this for testing
character = passwd[0:]
lcase_bad = False
for character in passwd:
if not character.islower() > 1:
lcase_bad = True
if lcase_bad:
print('Password must include lowercase letters ')
else:
print('password accepted')
for character in passwd:
Here you're iterating through each letter of the input.
When you do if not character.islower() > 1:, it will always be True. .islower() returns either True or False, depending on if the string is a capital letter or not. not False == 1, because boolean is a subclass of int. not True == 0. Both are not greater than one.
You can just do something like:
capital = False
lowercase = False
number = False
if len(passwd) < 6:
print 'That was not more than 6 characters'
else:
for character in passwd:
if character.islower():
lowercase = True
elif character.isupper():
capital = True
elif character.isdigit():
number = True
if capital and lowercase and number:
break
else:
print 'That did not have a capital letter, lowercase letter, and a digit'
Of course this is useful if you want to tell the person what the password didn't have. However, you can also just do one test instead.
Just check for all those conditions one after another:
mystring = input("enter password: ")
if any(c.isupper() for c in mystring) \ # There is an uppercase letter
and any(c.islower() for c in mystring) \ # There is a lowercase letter
and any(c.isdigit() for c in mystring) \ # There is a number
and len(mystring) > 5: # The length is 6 or greater
# string passed all tests
else:
# One or more tests failed--input is bad.
You've almost got it! If you remove the > 1 from your code (which won't really do anything useful), you get this:
lcase_bad = False
for character in passwd:
if not character.islower():
lcase_bad = True
It just happens that this will test to see if the entire string is made of lowercase letters. If it is, lcase_bad will remain False; otherwise, it will become True. It should not be an extreme leap of faith to see that if you flip the False and True around and call it lcase_good, you can see whether at least one character is lowercase.
As iCodez notes, you can also rewrite it using any with a generator comprehension. It reads fairly easily:
if any(character.islower() for character in passwd):
However, you probably haven't gotten to generator comprehensions, so it might be best to stay with a for loop for clarity's sake.

Categories