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
#7
import random

data = ["orange", "mango", "fridge", "mad", "smart", "love", "curtain", "chair", "table", "cable", "cake", "magic",
        "rapid", "talcum", "absent", "music", "radio", "house", "sofa", "kitchen", "napkin",
        "bucket", "awkward", "floor", "ceiling", "television", "handbag", "washbasin", "door", "window", "playground",
        "cricket", "basketball", "vehicle", "driver", "mirror", "bathroom", "photograph", "window"]

print(
    """         
                Welcome to HANGMAN game
         Guess alphabets to find the right word
       you got same chances as count of alphabets
              Enter 'play' - to start game
              Enter 'quit' - to quit game
    """)
player_wish = input("Enter your choice: ").lower()
while player_wish == "play":

    Name = data.pop(random.randint(1, len(data) - 1))
    word = [j for j in Name]
    word2 = word.copy()
    hangman = []
    for i in range(len(word)):
        hangman.append("_")

    guess_count = len(word) - 1
    attempt = []

    while hangman != word or guess_count == 0:
        print("Guess this word")
        print(' '.join(hangman))
        print("\n")
        print(f"You got {guess_count} chances left")
        alphabet = input("guess an alphabet: ").lower()
        attempt.append(alphabet)
        if alphabet in word:
            correct_index = word.index(alphabet)
            hangman.pop(correct_index)
            hangman.insert(correct_index, alphabet)
            while alphabet in word2:
                correct_index2 = word2.index(alphabet)
                word2.pop(correct_index2)
                word2.insert(correct_index2, "*")
                hangman.pop(correct_index2)
                hangman.insert(correct_index2, alphabet)
            print(f"\nYou got this one correct\nand \nyour attempted letters : {attempt}")
        else:
            print("\noops! wrong answer")
            print(f"You have {guess_count} chance left\n your attemped letters : {attempt}")
            if guess_count == 0:
                break
            guess_count -= 1
    if guess_count != 0:
        print("you won!!\nYou guessed the word correctly")
        print(' '.join(hangman))
        print("Want to play again?")
        player_desire = input("Enter 'yes' or 'no': ").lower()
        if player_desire == "yes":
            player_wish = "play"
        else:
            player_wish = "quit"
            print("Thanks for playing.")
    else:
        print(f"sorry, you lost the game\n The word was  '{' '.join(word)}' ")
        print("Want to play again?")
        player_desire = input("Enter 'yes' or 'no': ").lower()
        if player_desire == "y":
            player_wish = "play"
        else:
            player_wish = "quit"
            print("Thanks for playing.")
I have made hangman with little more interactive and can play over and over.
Professional Dentist(32years) fell in love with Python during COVID-19 Lockdown.

"Nothing can stop you from learning new things except your own will"

Reply


Messages In This Thread
RE: else condition not called when if condition is false - by Shahmadhur13 - Apr-24-2020, 02:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use isintance with condition? Azdaghost 3 641 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,223 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  unable to remove all elements from list based on a condition sg_python 3 1,832 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 1,491 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Multiple variable inputs when only one is called for ChrisDall 2 1,356 Oct-20-2023, 07:43 PM
Last Post: deanhystad
  Sent email based on if condition stewietopg 1 1,798 Mar-15-2023, 08:54 AM
Last Post: menator01
  Replacing values ​​in Mysql with a condition stsxbel 0 1,170 Mar-05-2023, 08:20 PM
Last Post: stsxbel
  Couldn't install a go-game called dlgo Nomamesse 14 6,961 Jan-05-2023, 06:38 PM
Last Post: Nomamesse
  create new column based on condition arvin 12 4,818 Dec-13-2022, 04:53 PM
Last Post: jefsummers
Question Running an action only if time condition is met alexbca 5 2,561 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