Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Leen Leenaerts
PLUS
Leen Leenaerts
Courses Plus Student 2,367 Points

need help with my first hangman program (didn't and don't want to look to solution before this one works)

import random


def pick_state():
#returns a state

    state_list = "Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware, District of Columbia, Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana, Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana, Nebraska, Nevada, New Hampshire, New Jersey, New Mexico, New York, North Carolina, North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina, South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia, Wisconsin, Wyoming".split(", ")
    return state_list[random.randint(0,len(state_list)-1)]


def guess():
    letter = input("guess a letter: ")
    global guessed_letters = global guessed_letters + letter

    if letter in secret_state and letter not in guessed_state:
      print("good")
    else:
      print("wrong")

    for alpha in "azertyuiopqsdfghjklmwxcvbn":
                   if alpha not in guessed_letters:
                       secret_state = secret_state.replace(alpha, "_")

return secret_state




def game():
  global secret_state = pick_state()
  global guessed_letters = ""
  global guessed_state = "_" * len(secret_state)


  while True:
      global guessed_state = guess()
      print(guessed_state)
            if guessed_state == secret_state:
                 break









game()

i'm getting a syntax error on line 13 ->

global guessed_letters = global guessed_letters + letter

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,986 Points

You are using the keyword global in an assignment. Use two statements.

global guessed_letters
guessed_letters = guessed_letters + letter

Also in main(), separate global declarations from assignment statements:

def game():
  global secret_state
  global guessed_letters
  global guessed_state

  secret_state = pick_state()
  guessed_letters = ""
  guessed_state = "_" * len(secret_state)