Question

In: Computer Science

In PYTHON: Write a program that asks for a password from a user and checks if...

In PYTHON:

Write a program that asks for a password from a user and checks if it satisfies the conditions:

  • Contains at least one lowercase letter (a-z).
  • Contains at least one uppercase letter (A-Z).
  • Contains at least one digit (0-9).
  • Minimum length 6 characters.
  • Contains no spaces.

If the password is valid, then the program prints “password accepted”. Otherwise, it keeps asking for more passwords from the user until a valid password is obtained.

Hint: You might like to check the string methods that start with the letter ‘i’.

Solutions

Expert Solution

# do comment if any problem arises

# Code


# this method returns true if given password has atleast one lowercase letter

def has_lower(password):

    for char in password:

        if char.islower():

            return True

    return False


# this method returns true if given password has atleast one uppercase letter

def has_upper(password):

    for char in password:

        if char.isupper():

            return True

    return False


# this method returns true if given password has atleast one digit

def has_digit(password):

    for char in password:

        if char.isnumeric():

            return True

    return False


# this method returns true if given password is valid

def isValid(password):

    # if length of password is less than 6

    if len(password) < 6:

        return False

    # if password contains spaces

    if ' ' in password:

        return False

    # if password doesnot has lowercase letter

    if not has_lower(password):

        return False

    # if password doesnot has uppercase letter

    if not has_upper(password):

        return False

    # if password doesnot has digit

    if not has_digit(password):

        return False

    # if all above conditions are satisfied then password is valid

    return True


def main():

    password = input("Enter a password: ")

    # while password is not valid

    while not isValid(password):

        print("Invalid password!\n")

        # reprompt for password

        password = input("Enter a password: ")

    print("Password accepted")


main()

Screenshot of code:

Output:


Related Solutions

Write Python code that asks the user for a password from the console and continues asking...
Write Python code that asks the user for a password from the console and continues asking for a password until the user enters a password that has greater than 10 characters. (language python)
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
PYTHON Write a program that accepts a range of input from the user and checks whether...
PYTHON Write a program that accepts a range of input from the user and checks whether the input data is sorted or not. If the data series is already sorted your program should print “True” or should print “False” otherwise. You should not use any sort function for this program. Input: How many numbers you want to input: 3 # user input 3 Input the number: 5 Input the number: 2 Input the number: 7 Output: False
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
In.java Write a program that repeatedly asks the user to enter their password until they enter...
In.java Write a program that repeatedly asks the user to enter their password until they enter the correct one. However, after 5 failed attempts, the program "locks out" the user. We will show that with an error message. Assume the password is HOC2141 (case sensitive). Note that there is a special case that is not shown below. To identify it, think of all possible scenarios of input for this program. ----------- Sample run 1: Enter your password: Blake Wrong Enter...
In Python write a program that asks the user to enter the monthly costs for the...
In Python write a program that asks the user to enter the monthly costs for the following expenses incurred from operating his or her automobile: loan payment, insurance, gas, oil, tires, and maintenance the program should then display the total monthly cost of these expenses, and the total annual cost of these expenses. your program MUST have BOTH a main function AND a function named calcExpenses to calculate the expenses. DO NOT display the expenses inside of the calcExpenses function!!...
Write a C++ program that checks if the password is correct. The password is a 4-digit...
Write a C++ program that checks if the password is correct. The password is a 4-digit number combination. The program repeats to ask the password until the password is correct or you enter -1 to exit. Input: The password is set to ‘1123’. A user input the password to proceed, or -1 to exit. Sample Output: The program should display the following output. (The red text is a user input.) Test case 1: when you enter the correct password. Enter...
Please write in python Use modular design to write a program that asks the user to...
Please write in python Use modular design to write a program that asks the user to enter his or her weight and the name of a planet. The program then outputs how much the user would weigh on that planet. The following table gives the factor by which the weight must be multiplied for each planet. PLANET CONVERSION FACTOR Mercury 0.4155 Venus 0.8975 Earth 1.0000 Moon 0.1660 Mars 0.3507 Jupiter 2.5374 Saturn 1.0677 Uranus 0.8947 Neptune 1.1794 Pluto 0.0899 The...
Write a design algorithm and a python program which asks the user for the length of...
Write a design algorithm and a python program which asks the user for the length of the three sides of two triangles. It should compute the perimeter of the two triangles and display it. It should also display which triangle has the greater perimeter. If both have the same perimeter it should display that the perimeters are the same. Formula: Perimeter of triangleA = (side1A + side2A +side3A) See the 2 sample outputs. Enter side1 of TriangleA: 2 Enter side2...
Python English algorithm explanation Write a program that asks the user for the name of a...
Python English algorithm explanation Write a program that asks the user for the name of a file in the current directory. Then, open the file and process the content of the file. 1)If the file contains words that appear more than once, print “We found duplicates.” 2)If the file does not contain duplicate words, print “There are no duplicates.”
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT