Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
October 4th, 2012, 05:39 PM
 mcdom34
Registered User

Join Date: Oct 2012
Posts: 1
Time spent in forums: 9 m 9 sec
Reputation 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
October 4th, 2012, 06:07 PM
 WynnDeezl
Contributing User

Join Date: Aug 2011
Posts: 139
Time spent in forums: 2 Days 3 h 53 m 35 sec
Reputation 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 06:21 PM.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Conditional Statements