Python Forum
else condition not called when if condition is false
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
else condition not called when if condition is false
#4
Answer is the same though. When the guess is not in the list index throws an exception.

There is no reason the word can't be a string. The word doesn't change. Here's a "Wheel of Fortune" version of Hangman. It matches multiple positions if a letter occurs in the word more than once.
import random

def find_all(letter, word):
    """Return array of indices where letter appears in word"""
    return [i for i, x in enumerate(word) if x == letter]

def hangman(word):
    """Play a game of hangman"""
    # Initialize to empty board
    word = word.upper()
    count = len(word)
    letters = ['_']*count
    remaining = count
    misses = 0

    # Begin play.  Play continues until player guesses the word or runs out of misses
    while remaining > 0 and misses < 6:
        """Draw the letters and remaining misses.  Get letter guess"""
        print(' '.join(letters))
        letter = input(f'Misses = {misses}.  Enter letter: ').upper()

        """Find all matches"""
        matches = find_all(letter, word)
        if len(matches) == 0:
            misses += 1  # No match
        else:
            for i in matches:
                letters[i] = letter # Replace blanks with letter
            remaining -= len(matches)

    # Did they win?
    if remaining == 0:
          print('Congratulations!')
    print(f'The word was "{word}"\n')
        

# Allow for multiple plays
words = ['Orangatan', 'Hippopotomus', 'Giraffe', 'Cheetah', 'Chimpanzee']
random.shuffle(words)

# Play until out of words or the player wants to quit.
first = True
for word in words:
    if not first:
        if input('Would you like to play again? ').upper()[0] != 'Y':
              break
    first = False
    hangman(word)
Reply


Messages In This Thread
RE: else condition not called when if condition is false - by deanhystad - Apr-20-2020, 03:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use isintance with condition? Azdaghost 3 661 May-08-2025, 01:22 PM
Last Post: buran
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 2,234 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  unable to remove all elements from list based on a condition sg_python 3 1,841 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 1,501 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Multiple variable inputs when only one is called for ChrisDall 2 1,366 Oct-20-2023, 07:43 PM
Last Post: deanhystad
  Sent email based on if condition stewietopg 1 1,808 Mar-15-2023, 08:54 AM
Last Post: menator01
  Replacing values ​​in Mysql with a condition stsxbel 0 1,180 Mar-05-2023, 08:20 PM
Last Post: stsxbel
  Couldn't install a go-game called dlgo Nomamesse 14 7,005 Jan-05-2023, 06:38 PM
Last Post: Nomamesse
  create new column based on condition arvin 12 4,857 Dec-13-2022, 04:53 PM
Last Post: jefsummers
Question Running an action only if time condition is met alexbca 5 2,590 Oct-27-2022, 02:15 PM
Last Post: alexbca

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020