It's a (simple) guesser for game where you need to find words of a given length only using given letters. I'd like to know if there's a more pythonic way to do things here.
#!/usr/bin/env python3
import sys
if len(sys.argv) < 3:
sys.exit("Usage: " + sys.argv[0] + " letters number_of_letters [dictionnaryfile]")
letters = sys.argv[1].upper()
wordsize = int(sys.argv[2])
dictionnary_name = "dict.txt" if len(sys.argv) < 4 else sys.argv[3]
try:
wordlist = open(dictionnary_name).read().split('\n')
except FileNotFoundError:
sys.exit("Couldn't find dictionnary file \"" + dictionnary_name + "\"")
good = []
for w in wordlist:
if len(w) == wordsize:
up = w.upper()
ok = True
for c in up:
if not c in letters:
ok = False
break
if ok:
good.append(w)
print("Found " + str(len(good)) + " results:")
print(good)