What improvements can I make? Should I organize my functions based on A-Z? For example, find_letter function comes before guessing function.
import json
import random
import re
def find_letter(user_input, vocab, i=None):
return [i.start() for i in re.finditer(user_input, vocab)]
def guessing(tries,vocab,blank_line):
while tries > 0:
user_input = input("Guess:")
if user_input in vocab:
if not(user_input in blank_line):
index = find_letter(user_input=user_input,vocab=vocab)
i = 0
while i < len(index):
blank_line[index[i]] = user_input
i += 1
if not('_' in blank_line):
return "you won!!!"
else:
print("YOU ALREADY TRIED THAT LETTER!")
else:
tries -= 1
print(str(blank_line) + " Number of Tries : " + str(tries))
return "you lost"
def hangman_game(vocab):
'Preliminary Conditions'
print(introductory(vocab=vocab))
tries = num_of_tries(vocab=vocab)
blank_line = produce_blank_lines(vocab=vocab)
print(blank_line)
print(guessing(tries=tries,vocab=vocab,blank_line=blank_line))
def introductory(vocab):
return "HANGMAN GAME. So...what you gotta do is guess and infer what the word might be. " \
"WORD COUNT : " + str(len(vocab))
def num_of_tries(vocab):
if len(vocab) < 7:
return 8
elif 7 <= len(vocab) < 10:
return 10
else:
return 14
def produce_blank_lines(vocab):
blank_line = []
for i in vocab:
if i.isalpha():
blank_line += "_"
elif i == " ":
blank_line += " "
else:
blank_line += i + " "
return blank_line
if __name__ == '__main__':
data = json.load(open("vocabulary.json"))
vocab = random.choice(list(data.keys())).lower()
print(vocab)
print(hangman_game(vocab=vocab))
Snippet of the JSON File
{"abandoned industrial site": ["Site that cannot be used for any purpose, being contaminated by pollutants."], "abandoned vehicle": ["A vehicle that has been discarded in the environment, urban or otherwise, often found wrecked, destroyed, damaged or with a major component part stolen or missing."], "abiotic factor": ["Physical, chemical and other non-living environmental factor."], "access road": ["Any street or narrow stretch of paved surface that leads to a specific destination, such as a main highway."], "access to the sea": ["The ability to bring goods to and from a port that is able to harbor sea faring vessels."], "accident": ["An unexpected, unfortunate mishap, failure or loss with the potential for harming human life, property or the environment.", "An event that happens suddenly or by chance without an apparent cause."], "accumulator": ["A rechargeable device for storing electrical energy in the form of chemical energy, consisting of one or more separate secondary cells.\\n(Source: CED)"], "acidification": ["Addition of an acid to a solution until the pH falls below 7."], "acidity": ["The state of being acid that is of being capable of transferring a hydrogen ion in solution."], "acidity degree": ["The amount of acid present in a solution, often expressed in terms of pH."], "acid rain": ["Rain having a pH less than 5.6."]
hangman.py
; or once that gets too big, you'd put everything related to the user interface inhangman-ui.py
and everything related to core gameplay inhangman-core.py
; or once that gets too big, you'd split outhangman-dictionary.py
into its own file... And then sure, I'd probably sort those files alphabetically in my manifest or Makefile or whatever. \$\endgroup\$introductory()
) unless I already know how to spell it! And if I know how to spell it (say, my IDE has a sidebar index of all the functions in the current file) then I likely don't care where in the file it is (since I can jump there using my IDE). But everyone has an "IDE" for the filesystem (ls
), and the filenames themselves make a rough "index by topic" — dictionary problem solved! \$\endgroup\$