Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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 68,423 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)