0

I am currently trying to make a hangman game. I have defined the variable 'lives' outside any functions, yet when trying to use it inside the start_game function the editor says that the variable is defined but never used. However when I try to declare it as global, whether it be inside or outside the function, it gives me an 'invalid syntax' error- specifically at the assignment operator '='.

import random

words = "dog cat log hog etc"     # <-- just a huge string of a bunch of words

words = words.split()

word = random.choice(words)


# Difficulties: Easy:12 Medium:9 Hard:6
lives = 0

current = "_" * len(word)


def gameLoop():
  while current != word and lives > 0:
    print("Guess a letter. If you wish to exit the game, enter 'exit'")
    input("")
    print(lives)


def start_game():
  while True:
    print("Welcome to Hangman! What game mode would you like to play? Easy, medium, or hard?")
    game_mode = str.lower(input(""))

   if game_mode == "easy":
      lives = 12
      gameLoop()
      break
    elif game_mode == "medium":
      lives = 9
      gameLoop()
      break
    elif game_mode == "hard":
      lives = 6
      gameLoop()
      break

start_game()

2 Answers2

1

While I was writing this question, I realized what I was doing wrong, so I decided to go ahead and answer this myself.

When you define a variable as global, you don't want to assign the variable a variable then and there like so:

global lives = 0

That will give you an error. Why? When you want to define a variable as global, you are telling the computer, "Hey, this variable right here is used globally, not locally." The issue with the above line of code is that you are also assigning the variable a value when all you should be doing at that point is telling the computer that the variable is global. If you want to assign a value to the variable (whether it's for the first time or a reassignment) then you need to assign the value on a different line of code.

When I looked this up I didn't find anything explicitly saying this, so I hope this helps out anyone new to coding with python or someone who forgot how it worked like I did.

  • 1
    You need to declare a name is being used globally in the scope it is being used in, not the scope it is from. `global lives` should go in `start_game`, not outside the function. – Patrick Haugh Dec 12 '18 at 14:41
  • He has not said global lives = 0 anywhere in the code. lives = 0, as mentioned in the code asked by op, is valid. He just has to declare the variable as global inside start_game() function. Coz, as soon as you assign a different value to a global variable inside a function, python decides that variable is local to the function. – Dhanya SJ Dec 12 '18 at 15:01
0

First of all, the global statement is a declaration, not an executable statement. It simply tells the interpreter to look in the module namespace rather than the function-call namespace. It need only be used inside a function.

Outside, the local and the global namespaces are the same thing (the module namespace), so the global statement does nothing.

The statement must be the keyword global followed by a comma-separated list of names to be treated as global. If you want to assign a value to any name, global or not, you must do so in a separate assignment statement.

You perhaps want something more like the code below, which will "work" (I realise this is only a partial program in development) as you want it. I fixed the indentation to conform to PEP 8, as my old eyes found it too hard to read the code otherwise!

import random

words = "tom dick harry".split()
word = random.choice(words)


# Difficulties: Easy:12 Medium:9 Hard:6
lives = 0

current = "_" * len(word)


def gameLoop():
    global lives
    while current != word and lives > 0:
        print("Guess a letter. If you wish to exit the game, enter 'exit'")
        input("")
        print(lives)


def start_game():
    global lives
    while True:
        print(
            "Welcome to Hangman! What game mode would you like to play? Easy, medium, or hard?"
        )
        game_mode = str.lower(input(""))

        if game_mode == "easy":
            lives = 12
            gameLoop()
            break
        elif game_mode == "medium":
            lives = 9
            gameLoop()
            break
        elif game_mode == "hard":
            lives = 6
            gameLoop()
            break


start_game()
holdenweb
  • 33,305
  • 7
  • 57
  • 77