I've almost finished coding my hangman game in Trinket using Python and I decided to add a visual aspect so it wasn't as boring. I soon ran into the problem that when I added 1 to a 'fail' variable it would hold it's value until the 'for loop' repeated. I haven't added the list of words because it would take up too much space here and isn't relevant. Here's my code in it's entirety:
# Prints the visual scaffold
def print_scaffold(guessed, fail):
if fail == 0:
print ""
print ""
print ""
print ""
print ""
print ""
print "________"
elif fail == 1:
print ""
print ""
print ""
print ""
print ""
print ""
print "|________"
elif fail == 2:
print ""
print ""
print ""
print ""
print ""
print "|"
print "|________"
elif fail == 3:
print ""
print ""
print ""
print ""
print "|"
print "|"
print "|________"
elif fail == 4:
print ""
print ""
print ""
print "|"
print "|"
print "|"
print "|________"
elif fail == 5:
print ""
print ""
print "|"
print "|"
print "|"
print "|"
print "|________"
elif fail == 6:
print ""
print "|"
print "|"
print "|"
print "|"
print "|"
print "|________"
elif fail == 7:
print "_________"
print "|"
print "|"
print "|"
print "|"
print "|"
print "|________"
elif fail == 8:
print "_________"
print "| |"
print "|"
print "|"
print "|"
print "|"
print "|________"
elif fail == 9:
print "_________"
print "| |"
print "| O"
print "|"
print "|"
print "|"
print "|________"
elif fail == 10:
print "_________"
print "| |"
print "| O"
print "| |"
print "|"
print "|"
print "|________"
elif fail == 11:
print "_________"
print "| |"
print "| O"
print "| \|"
print "|"
print "|"
print "|________"
elif fail == 12:
print "_________"
print "| |"
print "| O"
print "| \|/"
print "|"
print "|"
print "|________"
elif fail == 13:
print "_________"
print "| |"
print "| O"
print "| \|/"
print "| |"
print "|"
print "|________"
elif fail == 14:
print "_________"
print "| |"
print "| O"
print "| \|/"
print "| |"
print "| /"
print "|________"
elif fail == 15:
print "_________"
print "| |"
print "| O"
print "| \|/"
print "| |"
print "| / \ "
print "|________ "
guessed = True
return guessed, fail;
# Tests whether or not your guess was correct
def check(word, guesses, guess, fail):
status = ''
matches = 0
for letter in word:
if letter in guesses:
status += letter
else:
status += ' _ '
if letter == guess:
matches += 1
if matches > 1:
print('Yes! The word contains ' + str(matches) + ' "' + guess + '"' + 's.')
elif matches == 1:
print('Yes! The word contains the letter "' + guess + '".')
else:
print('Sorry. The word does not contain the letter "' + guess + '".')
fail = fail + 1
print(status)
print(fail) # I put this here to see if the varaible held it's value after the 'IF' statement
return status, fail;
# Main part of the game; this is what runs
def main():
# Chooses a random word from a list of 200
word = random.choice(dictionary).upper()
guesses = []
fail = 0
guessed = False
print("The word contains " + str(len(word)) + " letters.")
while not guessed:
text = '\nPlease enter one letter or a {}-letter word. '.format(len(word))
print(fail) # Shows if the varaible held it's value entering the repeat
print_scaffold(guessed, fail)
print(fail) # Shows if the varaible held it's value after the function
guess = input(text)
guess = guess.upper()
if guess in guesses:
print('You already guessed "' + guess + '".')
elif len(guess) == len(word):
guesses.append(guess)
if guess == word:
guessed = True
else:
print("Sorry, that is incorrect.")
elif len(guess) == 1:
guesses.append(guess)
print(fail) # Shows if the varaible held it's value before it was updated
result = check(word, guesses, guess, fail)
if result == word:
guessed = True
else:
print("Invalid entry.")
print("\nYes, the word is "+ word + "! Good Job!")
main()
This is what it outputs if you enter a wrong letter. FYI when the player answers a letter correctly it replaces it's corresponding underscore with the letter. The numbers show the value of the 'fail' variable. The 1 is before the repeat but the 0 after it is the 'fail' variable's value after the repeat.
The word contains 8 letters.
0
________
0
Please enter one letter or a 8-letter word. a
0
Sorry. The word does not contain the letter "A".
_ _ _ _ _ _ _ _
1
0
________
0
Please enter one letter or a 8-letter word.