Python| Random Password Generator using Tkinter With growing technology, everything has relied on data and securing these data is the main concern. Passwords are meant to keep the data safe that we upload on the Internet. Import the random module into the program. Take the number of elements from the user. Use a for loop, random.randint() is used to generate random numbers which are them appending to a list. Then print the randomised list. Here is source code of the Python Program to generate random numbers from 1 to 20 and append them to the list. Google apps for laptops free. • You can make check_upper, check_lower, etc all use one function, and so you want to make a function such as check_contains(input, letters). Which can further be improved by: • Returning early by using return True if char in letters is true. • You can make this a comprehension. • You can use any to achieve the same as (1) when using (2). And so I'd use: def check_contains(input, letters): return any(char in letters for char in input) • I'd personally make validate_password(input) only return true or false, however to keep with what you're doing, I'll keep it so that it prints. • Remove sys.exit, it's not intended to be used in programs. Instead use break to break out of the while loop. • I'd use, rather than raw_input to get the users password. This is as it should turn echo off, and so won't display the users password, so others can shoulder serf for their password. • Rather than manually writing out the strings, you can use. And so I'd change your code to: from getpass import getpass import string def check_contains(input, letters): return any(char in letters for char in input) def validate_password(input): valid = True if not check_contains(input, string.ascii_uppercase): valid = False print 'Password needs at least one upper-case character.' If not check_contains(input, string.ascii_lowercase): valid = False print 'Password needs at least one lower-case character.' If not check_contains(input, string.digits): valid = False print 'Password needs at least one number.' If not check_contains(input, string.punctuation + '#'): valid = False print 'Password needs at least one special character.' If len(input) = 8 ]) while True: password = getpass('Enter desired password: ') if validate_password(password): print 'Valid password' break else: print 'invalid password'.
0 Comments
Leave a Reply. |