0

I'm trying to make a dumbed down version of hangman to practice python, I have a count variable but it isn't counting when the condition is met, what am I doing wrong

running = True
words = ['test']
cWord = random.choice(words)
cLength =len(cWord)
count = 0
print("I have chosen a word!")
print("It is a " + str(cLength) + ' letter word')
while running:
  userGuess = input("type to guess")

  def letterCheck(cWord, userGuess):
    for x in cWord:
      if userGuess == x:
        print(len(x))
    if userGuess != x:
      counter(count)

  def wordCheck(cWord, userGuess, running):
    if userGuess != cWord:
      pass
    if userGuess == cWord:
      print('Correct!/n The word was ' + cWord)
      running = False
  def counter(count):
    count += 1
    print('Number of tries ' + str(count))
    return count
    
  
  
  letterCheck(cWord, userGuess)
  wordCheck(cWord,userGuess,running)
Weku
  • 3
  • 2

1 Answers1

1

The problem you are facing is that by passing the count variable, you create a copy within the local variables of the counter function. This means the the local count gets incremented, by not the one outside the function.

Pput count as a global variable. This is a depreciated method as you would change a variable state outside the function and it become unclear where the change come from, but this will be the easiest fix considering your current code

import random

running = True
words = ['test']
cWord = random.choice(words)
cLength = len(cWord)
count = 0
print("I have chosen a word!")
print("It is a " + str(cLength) + ' letter word')
while running:
    userGuess = input("type to guess")

    def letterCheck(cWord, userGuess):
        for x in cWord:
            if userGuess == x:
                print(len(x))
        if userGuess != x:
            counter()


    def wordCheck(cWord, userGuess, running):
        if userGuess != cWord:
            pass
        if userGuess == cWord:
            print('Correct!/n The word was ' + cWord)
            running = False


    def counter():
        global count
        count += 1
        print('Number of tries ' + str(count))
        return count


    letterCheck(cWord, userGuess)
    wordCheck(cWord, userGuess, running)

Here is the result:

enter image description here

Yohann Boniface
  • 494
  • 3
  • 10