0

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. 
Jarryd
  • 1
  • Python does not pass integer variables by reference. Some links: [1](https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference), [2](https://robertheaton.com/2014/02/09/pythons-pass-by-object-reference-as-explained-by-philip-k-dick/) [3](https://stackoverflow.com/questions/534375/passing-values-in-python) – ジョージ Apr 17 '20 at 10:28
  • Back to your code -- you may want to make `fail` a global variable ; this would be a little ugly, but will work ; for that, (a) initialize it somewhere outside all function definitions ( e.g. on the second line of your file ) and (b) say `global file` somewhere at the start of your function definitions . ( This way, you won't need to keep `file` a function argument any more. ) Of course there are other ways, see e.g. [4](https://www.programiz.com/python-programming/global-local-nonlocal-variables) – ジョージ Apr 17 '20 at 10:38
  • 1
    That fixed the issue, everything is working. Thank you so much! – Jarryd Apr 17 '20 at 11:25

1 Answers1

0

I made 'fail' a global variable which allowed me to use it outside of the defined functions. - thanks to ジョージ

Jarryd
  • 1