diff --git a/hangman.py b/hangman.py index a7725cc..19132e2 100644 --- a/hangman.py +++ b/hangman.py @@ -6,11 +6,14 @@ total_score = 0 - +# words: list that stores all words in the collection +# lives: integer that stores the total lives a user has (based on the level chosen) def get_valid_word(words, lives): """Gets word from words.py wihtout '-' or ' '.""" word = "" + # makes a new list of words based on their lengths + # and then chooses a word randomly from the new list if lives == 12: word_dict = [word for word in words if len(word) >= 4 and len(word) <= 6] word = random.choice(word_dict) @@ -24,7 +27,7 @@ def get_valid_word(words, lives): word_dict = [word for word in words if len(word) > 10] word = random.choice(word_dict) - # check for foul words + # until a word contains ' ' or '-', choose another word randomly while '-' in word or ' ' in word: word = random.choice(word_dict) @@ -36,12 +39,14 @@ def hangman(): global total_score - lives = ask_for_level() - word = get_valid_word(words, lives) - word_letters = set(word) - alphabet = set(string.ascii_uppercase) - used_letters = set() + lives = ask_for_level() # saves the number of lives + word = get_valid_word(words, lives) # gets a random word based on difficulty + word_letters = set(word) # makes a set of the letters of randomly chosen word + alphabet = set(string.ascii_uppercase) #stores A-Z in alphabet + used_letters = set() # makes an empty set to store used letters + # stores dictionary of lives visual (from hangman_visual.py) in visual + # according to the difficulty of the game if lives == 12: visual = lives_visual_dict_easy point = int(1) @@ -55,30 +60,33 @@ def hangman(): visual = lives_visual_dict_impossible point = int(15) + # the loop breaks when either the lives become zero or the word is guessed while len(word_letters) > 0 and lives > 0: - + + # prints the number of lives remaining and letters used print(f"\nYou have {lives} lives left and you have used the letters:", " ".join(used_letters)) + # prints the visual and letters guessed correctly in the word word_list = [letter if letter in used_letters else '-' for letter in word] print(visual[lives]) print(f"Current word: ", " ".join(word_list)) user_letter = input("Guess a letter: ").upper() - if user_letter in alphabet - used_letters: + if user_letter in alphabet - used_letters: used_letters.add(user_letter) - if user_letter in word_letters: + if user_letter in word_letters: # if the input letter is the right guess word_letters.remove(user_letter) print('') - else: + else: # if the input letter is not the right guess lives = lives - 1 print(f"\nYou have guessed the wrong letter... {user_letter} not in the word.") - elif user_letter in used_letters: + elif user_letter in used_letters: # if the letter is already inputted print("\nYou have already guessed that number.") - else: + else: # invalid letter print(f"{user_letter} is not a valid input.") # gets here when len(word_letters) == 0 or lives == 0 @@ -135,6 +143,7 @@ def ask_for_level(): lives accordingly. """ + # loop infinitely iterate until player chooses the difficulty i.e. e, m, h or i while True: print("\n1) Easy (e): Lives: 12") print("2) Medium (m): Lives: 9")