0

I'm tasked with doing this assignment in class, (I'm making code that checks if a password a user puts in follows guidelines).

A portion of this requires me to check if for example A is in B, but I'm having trouble. Here's a portion of the code, how would I check if an uppercase letter is in the password inputted? Sorry, I'm new to python and I'm trying to learn new concepts!

I looked online but what people are saying to put is a bit to complicated for me (as of now).

def valid_password(pw):
    up = 'ABCDEFGHIJKLMNOPQRSTUVWXWY'
    if pw in up
      return True  
TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65
FireFume
  • 59
  • 1
  • 1
  • 5
  • up:'ABCDEFGHIJKLMNOPQRSTUVWXWY' that doesn't work: – Jean-François Fabre Oct 28 '16 at 20:45
  • Is that your real code? Because that is not valid Python syntax. – idjaw Oct 28 '16 at 20:46
  • No, I was just putting that to ask how I would check if PW is in a set range of #'s, letters, or words. I've learned plenty of other concepts but I absolutely forgot I would do something like this – FireFume Oct 28 '16 at 20:46
  • http://stackoverflow.com/questions/9257094/how-to-change-a-string-into-uppercase – Bookeater Oct 28 '16 at 20:49
  • http://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method – Bookeater Oct 28 '16 at 20:49
  • Hint: you have to scan over the characters in the password and check each one against up = 'ABCDEFGHIJKLMNOPQRSTUVWXWYZ'. You might want to have a function by itself just for that check. – Pat Jones Oct 28 '16 at 20:49
  • FireFume, you can easily fix that, read about variable assignment in Python. Don't forget the else condition if the string is not on 'up' – FelipeS Oct 28 '16 at 20:50
  • Yeah, I wasn't forgetting the else. The assignment requires alot of nested if statements though, so I'm trying to make sure each concept is right before putting it together! – FireFume Oct 28 '16 at 20:52
  • `pw in up` means to check if the entire password matches a substring of the alphabet. It will be true if `pw = 'CDE'`, for instance. Is that what you're trying to check? – Barmar Oct 28 '16 at 20:55

5 Answers5

2

Use any in combination with a comprehension:

>>> pw1 = "abcd"
>>> pw2 = "abcD"
>>> any(c.isupper() for c in pw1)
False
>>> any(c.isupper() for c in pw2)
True

c.isupper() evaluates to either True or False for each of the characters in the string. The syntax expr for var in iterable is called a generator comprehension and is an iterable. any takes an iterable and tells you whether any of its elements are True.

Given that, you can construct a function:

def valid_password(pw):
    return any(c.isupper() for c in pw)
dkasak
  • 2,651
  • 17
  • 26
1

If its just to check if pw has any uppercase characters, this method should work.

import re
def valid_password(pw):
   pattern = re.compile(r'[A-Z]')
   if pattern.findall(pw):
       return True
   else:
       return False
sandeep koduri
  • 500
  • 2
  • 15
0

if pw in up would not make sense in your current context, as conceptually you are checking if the entire password string pw is found as a substring in up. What you want to check is if any character found within pw is in up.

def valid_password(pw):
    up = 'ABCDEFGHIJKLMNOPQRSTUVWXWY'
    for letter in pw:
       if letter in up:
          return True

alternatively there is a built in 'isupper'

def valid_password(pw):
    for letter in pw:
       if letter.isupper():
          return True
TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65
0

Let's not forget the wonder that is the string module:

import string

def valid_password(pw):
    return any(c in string.ascii_uppercase for c in pw)

This is almost English already, but this checks to see if any characters in the password are in the collection of uppercase letters (string.ascii_uppercase) and returns True if any are and False otherwise.

Noah
  • 1,329
  • 11
  • 21
0

For this kind of situation best answer is regular expression look ahead and look behind. Take example i need to validate a password with below criteria.

  1. password must contains at-least 1 capital letter. here is the code
def valid_password(password):
    import re
    exp = r'(?=[A-Z]{1,}).*'
    match = re.search(exp, password)
    if match.group() :
        return True
    else:
        return False

print(valid_password("A1ybhcdhhj"))

Source : https://www.rexegg.com/regex-lookarounds.html