Welcome to the Treehouse Community
Looking to learn something new?
Leen LeenaertsCourses 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
Chris FreemanTreehouse Moderator 67,986 Points
You are using the keyword
global in an assignment. Use two statements.
global guessed_letters guessed_letters = guessed_letters + letter
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)