The code is overcomplicated and somewhat rough for what it does. Python docs has 4 line snippet that you just copypaste if you need:
import secrets
# On standard Linux systems, use a convenient dictionary file.
# Other platforms may need to provide their own word-list.
with open('/usr/share/dict/words') as f:
words = [word.strip() for word in f]
password = ' '.join(secrets.choice(words) for i in range(4))
https://docs.python.org/3/library/secrets.html#recipes-and-b...Also EFF had prepared convenient wordlists specifically for password generation: https://www.eff.org/deeplinks/2016/07/new-wordlists-random-p...