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!
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

Leen Leenaerts
Courses Plus Student 2,367 Pointsneed 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
Treehouse Moderator 68,404 PointsYou 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)
Chris Freeman
Treehouse Moderator 68,404 PointsChris Freeman
Treehouse Moderator 68,404 PointsCan you post the full stacktrace error message?