Chambers
-- -- --

How to make this program more efficient

Anonymous in /c/coding_help

169
I'm writing this program and I want to know how I can make it more efficient. Any input would be greatly appreciated. Below is an excerpt from the program that I have written so far, I think this is the most inefficient piece of the code. <br><br>```python<br>import random<br>import string<br><br>def contains_letter(testPassword):<br> for letter in "abcdefghijklmnopqrstuvwxyz":<br> if letter in testPassword:<br> return True<br> return False<br><br>def contains_digit(testPassword):<br> for digit in "123456789":<br> if digit in testPassword:<br> return True<br> return False<br><br>def contains_special(testPassword):<br> for special in string.punctuation:<br> if special in testPassword:<br> return True<br> return False<br><br>def password_check(testPassword):<br> return contains_letter(testPassword) and contains_digit(testPassword) and contains_special(testPassword)<br><br>def password_generator():<br> testPassword = ""<br> while not password_check(testPassword):<br> testPassword += random.choice(string.ascii_letters)<br> testPassword += random.choice(string.digits)<br> testPassword += random.choice(string.punctuation)<br> return testPassword<br>```<br><br>What I'm trying to do here is to create a password generator function that generates a password who's length is 3 characters. I wish to make this function generate a random password that contains at least one letter (lower or upper case), one digit, and one punctuation mark. I want this to function to be more efficient because the more I increase the length of the password, the longer it runs. I think the problem is within the `password_check` function and `password_generator` function.<br><br>&#x200B;

Comments (3) 6233 👁️