Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
beginner questions
#1
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
Reply
#2
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)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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")
Reply
#5
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
#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")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner class. Need help with some questions. Willing to pay curtice 1 2,238 Mar-09-2018, 06:26 AM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020