Attempt to guess a password using random choices from printable chars. Parameters ---------- password : str The password to guess. Returns ------- str The correctly guessed password. Example: >>> random.seed(1) >>> guess_password_simulation
(password: str)
| 24 | |
| 25 | |
| 26 | def guess_password_simulation(password: str) -> str: |
| 27 | """ |
| 28 | Attempt to guess a password using random choices from printable chars. |
| 29 | |
| 30 | Parameters |
| 31 | ---------- |
| 32 | password : str |
| 33 | The password to guess. |
| 34 | |
| 35 | Returns |
| 36 | ------- |
| 37 | str |
| 38 | The correctly guessed password. |
| 39 | |
| 40 | Example: |
| 41 | >>> random.seed(1) |
| 42 | >>> guess_password_simulation("abc") # doctest: +ELLIPSIS |
| 43 | 'abc' |
| 44 | """ |
| 45 | chars_list: List[str] = list(string.printable) |
| 46 | guess: List[str] = [] |
| 47 | |
| 48 | attempts = 0 |
| 49 | while guess != list(password): |
| 50 | guess = random.choices(chars_list, k=len(password)) |
| 51 | attempts += 1 |
| 52 | print(f"<== Attempt {attempts}: {''.join(guess)} ==>") |
| 53 | |
| 54 | print("Password guessed successfully!") |
| 55 | return "".join(guess) |
| 56 | |
| 57 | |
| 58 | if __name__ == "__main__": |