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
#5
@deanhystad thanks for code. I m still understanding the functions and parameters in it. its pretty awesome. i was having trouble using strings.

as for my code, i removed index function from the if condition and rewrote the code. its working now. Both errors are addressed.

print("=====Hangman =====\nGuess the letters to fill the blanks")
w = ["f", "l", "a", "m", "e", "s"]
b = ["_", "_", "_", "_", "_", "_"]
wrong = 0

while b != w and wrong <6:
    print(b)
    x = input("Enter your guess:  ")
    if x in w:
        i = w.index(x)
        b[i] = x
    else:
        wrong += 1
        print("Wrong guesses : " + str(wrong))
        if wrong == 6:
            print("Your are out of guesses")
if b == w:
    print(b)
    print("\n You Win !!")
Output:
=====Hangman ===== Guess the letters to fill the blanks ['_', '_', '_', '_', '_', '_'] Enter your guess: a ['_', '_', 'a', '_', '_', '_'] Enter your guess: s ['_', '_', 'a', '_', '_', 's'] Enter your guess: q Wrong guesses : 1 ['_', '_', 'a', '_', '_', 's'] Enter your guess: w Wrong guesses : 2 ['_', '_', 'a', '_', '_', 's'] Enter your guess: e ['_', '_', 'a', '_', 'e', 's'] Enter your guess: f ['f', '_', 'a', '_', 'e', 's'] Enter your guess: l ['f', 'l', 'a', '_', 'e', 's'] Enter your guess: z Wrong guesses : 3 ['f', 'l', 'a', '_', 'e', 's'] Enter your guess: x Wrong guesses : 4 ['f', 'l', 'a', '_', 'e', 's'] Enter your guess: v Wrong guesses : 5 ['f', 'l', 'a', '_', 'e', 's'] Enter your guess: b Wrong guesses : 6 Your are out of guesses
Reply


Messages In This Thread
RE: else condition not called when if condition is false - by Sandz1286 - Apr-20-2020, 05:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 480 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  unable to remove all elements from list based on a condition sg_python 3 579 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 819 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Multiple variable inputs when only one is called for ChrisDall 2 553 Oct-20-2023, 07:43 PM
Last Post: deanhystad
  Sent email based on if condition stewietopg 1 959 Mar-15-2023, 08:54 AM
Last Post: menator01
  Replacing values ​​in Mysql with a condition stsxbel 0 680 Mar-05-2023, 08:20 PM
Last Post: stsxbel
  Couldn't install a go-game called dlgo Nomamesse 14 3,338 Jan-05-2023, 06:38 PM
Last Post: Nomamesse
  create new column based on condition arvin 12 2,481 Dec-13-2022, 04:53 PM
Last Post: jefsummers
Question Running an action only if time condition is met alexbca 5 1,421 Oct-27-2022, 02:15 PM
Last Post: alexbca
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 908 Sep-08-2022, 06:32 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

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