#hangman.py #using the words_merged.txt list from random import randint words = open("words_merged.txt").read().split() words.sort(key=lambda s: len(s)) print("#words:",len(words)) word_length = int(input("Length of word you want (1-22): ")) if word_length<1 or word_length>22: # antidisestablishmentarianism is out print("Invalid length. Quitting") exit() #start and stop index of words of this length: start = 0 while len(words[start]) < word_length: start += 1 stop = start while len(words[stop]) == word_length: stop += 1 print(words[start],words[stop-1], " #words this length:",stop-start) chosen_word = words[randint(start,stop-1)] print(chosen_word) #comment out this line if you really want to play Hangman user_string_list = ["_" for i in range(word_length)] print("Your progress:","".join(user_string_list)) guesses = 0 while user_string_list.count('_') > 0: #until no _'s guess_char = input("Guess a letter: ").lower() if not guess_char.isalpha() or len(guess_char)!=1: print("That's not a single letter") continue guesses += 1 hits = chosen_word.count(guess_char) #print("hits:",hits) if hits == 0: print("That letter is not in the word") else: i = -1 while hits > 0: i += 1 while chosen_word[i] != guess_char: i += 1 user_string_list[i] = guess_char hits -= 1 print("Your progress:","".join(user_string_list)) print("Congratulations") print("#guesses:",guesses)