October 4th, 2012, 05:39 PM
Hey! I'm trying to program a very simplistic hangman game, and in doing so, I have to break out of a while loop after so many right guesses or so many wrong guesses. The while loop ends if I have just one conditional statement, but when I include [conditional1 or conditional2] the loop continues infinitely. Is there any way I can remedy this? My code is posted below
a=['hello', 'goodbye', 'hola', 'chao']
while guessWrong < 7 or guessRight < len(a[n]):
g=raw_input("Guess a letter: ")
if(g in a[n]):
print "Word contains " + str(gr)
print "Incorrect guess\n"
print "You lost"
print "You win"
October 4th, 2012, 06:07 PM
You have to think about what the code actually does in the boolean comparisons.
One of these (or both) is always being True, so it's 1 OR 0, and 0 OR 1. Anything OR'ed with 1 will always give a 1, or True result.
while guessWrong < 7 or guessRight < len(a[n]) :
Last edited by WynnDeezl; October 4th, 2012 at 06:21 PM.