#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Conditional Statements


    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
    Code:
    import random
    
    a=['hello', 'goodbye', 'hola', 'chao']
    n=random.randrange(0, len(a))
    guessRight=0
    guessWrong=0
    
    while guessWrong < 7 or guessRight < len(a[n]):
        g=raw_input("Guess a letter: ")
        if(g in a[n]):
            guessRight+=1
            print "Word contains " + str(gr)
        else:
            guessWrong+=1
            print "Incorrect guess\n"
    
    if(guessWrong==7):
        print "You lost"
    else:
        print "You win"
    Thanks,
    mcdom34
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    139
    Rep Power
    3

    Boolean


    You have to think about what the code actually does in the boolean comparisons.

    Code:
    while guessWrong < 7 or guessRight < len(a[n]) :
    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.


    Last edited by WynnDeezl; October 4th, 2012 at 05:21 PM.

IMN logo majestic logo threadwatch logo seochat tools logo