Skip to content

Instantly share code, notes, and snippets.

@StevenMaude
Last active August 24, 2016 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save StevenMaude/2a222caed6592d6f631d to your computer and use it in GitHub Desktop.
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.
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()
@the-c0d3r
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment