# The User must beable to enter a valid password before they continue valid_password_entered = False # Loop until valid password is entered while not valid_password_entered: #Raw input is used so that the program leaves the input and does not #Try and interpret it pass_str = raw_input('Please enter your password: ') # To test if the password is the rigt length # I have used if statement if len(password_str) < 6: print 'Password:', password_str, 'too short! It must be between 6 and 12 characters.' elif len(password_str) > 12: print 'Password:', password_str, 'too long! It must be between 6 and 12 characters.' else: # Set flags to be set when different tests succeed lowercase_found = 0 uppercase_found = 0 numeric_found = 0 # Step through string and set flags where appropriate for ch in password_str: if ch.islower(): lowercase_found = 1 if ch.isupper(): uppercase_found = 1 if ch.isdigit(): numeric_found = 1 # Exit early if all our flags have been set if lowercase_found and uppercase_found and numeric_found: break # Calculate password strength, and print out result password_strength = lowercase_found + uppercase_found + numeric_found # If the password is either 1, 2 or 3, then print out valid message. if password_strength in range(1,4): print 'The password', password_str, 'is valid.' # Now print out password strength if password_strength == 1: print 'Password', password_str, 'is a WEAK password' elif password_strength == 2: print 'Password', password_str, 'is a MEDIUM password' else: print 'Password', password_str, 'is a STRONG password' # Set loop variable, so that we do not ask for password again valid_password_entered = True else: print 'Invalid password:', password_str, 'contained neither letters or numbers!'
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question