Last active
August 24, 2016 22:33
-
-
Save StevenMaude/2a222caed6592d6f631d to your computer and use it in GitHub Desktop.
Movie-like password cracking animation in Python that looks nifty. Code for guessing a string written for a problem in "Problem Solving with Algorithms and Data Structures" with my additional idea of printing every attempt and slowing down prints to make them readable. Tested with Python 2.7/3.4.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function, unicode_literals | |
import random | |
import time | |
def generate_random_guess(chars): | |
return random.choice(chars) | |
def repeated_guesses(target): | |
chars = 'abcdefghijklmnopqrstuvwxyz ' | |
current_guess = len(target) * ['*'] | |
for count, char in enumerate(target): | |
while current_guess[count] != char: | |
current_guess[count] = generate_random_guess(chars) | |
print(''.join(current_guess)) | |
# slight pause to make readable | |
time.sleep(0.015) | |
def main(): | |
target = "methinks it is like a weasel" | |
repeated_guesses(target) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please check out my edit
Thanks for the script. I've edited it to make it more "hollywood" like. I used
sys.stdout.flush(), sys.stdout.write()
and\r
carriage return.