Python Forum
beginner questions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: beginner questions (/thread-15456.html)



beginner questions - Naito - Jan-18-2019

hello, i have to create a code that makes a user input a password that have at least 10 "symbols", have 1 uppercase , one lowercase , and one digit
i statrted coding 1 week and i still haven't grasp the fundamentals Cry
can you help me please?

x = input("please enter your password: ")
userpassword = [x]
for i in userpassword[0]:
    if len(userpassword[0]) < 10:
        raise Exception("password must have at least 10 characters")
    elif not userpassword[0].isdigit():
        raise Exception("password must have at least 1 digit")
    elif not userpassword[0].islower():
        raise Exception("password must have at least one lowercase letter in it ")
    elif not userpassword[0].isupper():
        raise Exception("pass words must have at least one uppercase letter in it")
Output:
please enter your password: cecerceercrecer5cerercercer Traceback (most recent call last): File "D:/Python/Medi/Medi.py", line 6, in <module> raise Exception("password must have at least 1 digit") Exception: password must have at least 1 digit
thank you! Big Grin


RE: beginner questions - perfringo - Jan-18-2019

userpassword[0] is (whole) string. Therefore isdigit() can't be True:

>>> 'cecerceercrecer5cerercercer'.isdigit()
False
One way to have working solution is to combine built-in function any() and list comprehension (in this case it would be string comprehension)


RE: beginner questions - buran - Jan-18-2019

I would suggest you get back to the docs and read about isdigit(), islower() and isupper().
  • str.isdigit() - Return true if all characters in the string are digits and there is at least one character, false otherwise...
  • str.islower() - Return true if all cased characters in the string are lowercase and there is at least one cased character, false otherwise.
  • str.isupper() - Return true if all cased characters [4] in the string are uppercase and there is at least one cased character, false otherwise.

What you want to check is that at least one is digit, is lowercase or is uppercase
Look at any()

Also, you raise a general Exception. I would suggest to use more appropriate ValueError


RE: beginner questions - ODIS - Jan-18-2019

Possible solution:

def check_password(password: str):
    if len(password) < 10:
        raise Exception("password must have at least 10 characters")
    has_digit = False
    has_lowercase = False
    has_uppercase = False
    for letter in password:
        if letter.isdigit():
            has_digit = True
        elif letter.islower():
            has_lowercase = True
        elif letter.isupper():
            has_uppercase = True
    if not has_digit:
        raise Exception("password must have at least 1 digit")
    if not has_lowercase:
        raise Exception("password must have at least one lowercase letter in it")
    if not has_uppercase:
        raise Exception("password must have at least one uppercase letter in it")



RE: beginner questions - perfringo - Jan-18-2019

It' quite simple to translate English into Python.

What you want to do: I want to check whether password has minimum required length. If so I want to check whether it contains at least one letter in uppercase; at least one letter in lowercase; at least one letter is a number. If any of these requirements are not met raise error with appropriate information. If password meets requirements print that on screen.

In Python it can be expressed this way:

>>> password = input("Please enter your password: ")
>>> if len(password) < 10:                                              
...     raise ValueError('Password must have at least 10 characters!')
... elif not any(letter.isupper() for letter in password):
...     raise ValueError('Password must have at least one uppercase letter!')
... elif not any(letter.islower() for letter in password):
...     raise ValueError('Password must have at least one lowercase letter!')
... elif not any(letter.isdigit() for letter in password):
...     raise ValueError('Password must have at least one digit!')
... else:
...     print('Your password accepted!')
I think that it is hard to get any closer to spoken language than this Smile


RE: beginner questions - aakashjha001 - Jan-27-2019

#Check how each function like isdigit,isuppper etc works.



def check_password(password: str):
    if len(password) < 10:
        raise Exception("password must have at least 10 characters")
    has_digit = False
    has_lowercase = False
    has_uppercase = False
    for letter in password:
        if letter.isdigit():
            has_digit = True
        elif letter.islower():
            has_lowercase = True
        elif letter.isupper():
            has_uppercase = True
    if not has_digit:
        raise Exception("password must have at least 1 digit")
    if not has_lowercase:
        raise Exception("password must have at least one lowercase letter in it")
    if not has_uppercase:
        raise Exception("password must have at least one uppercase letter in it")
    else:
        print("{Password accepted")
check_password("aakashjha001")
check_password("alisA")