Skip to content

Instantly share code, notes, and snippets.

@juandesant
Last active February 19, 2021 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juandesant/61e4a37fd1b3bec675c4b5c4506fcf52 to your computer and use it in GitHub Desktop.
Save juandesant/61e4a37fd1b3bec675c4b5c4506fcf52 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Based on https://m.xkcd.com/936/
import random
dict_file = open('/usr/share/dict/words', 'r') # substitute with the path your /etc/dict/words equivalent
word_list = [x.rstrip('\n') for x in dict_file.readlines()]
capitalise_first_letter = True
capitalise_first_word = False
joiner_character = "-"
while True:
# We use ValueError and KeyboardInterrupt exceptions to break from this neverending loop.
try:
num_words = int(input("How many words to use for your password? ")) # Ctrl-C or non-numeric input to break out
words_in_sample = random.sample(word_list, num_words)
if capitalise_first_word:
words_in_sample[0] = words_in_sample[0].capitalize()
if capitalise_first_letter:
for i in range(0,len(words_in_sample[1:])):
words_in_sample[i+1] = words_in_sample[i+1].capitalize()
print(joiner_character.join(words_in_sample))
except ValueError as e:
break
except KeyboardInterrupt as e:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment