import random def wordlistcheck (): l = open(hangmanlist,"r"): words = l.readlines() for i in words: wordline = l.split(" ") def displayFileMenu (): print ("---File Menu---") print (" ") print ("1 - Load word list") print ("2 - Print word list") print ("3 - Return to main menu") filechoice = int(input("Please select an option: ") if filechoice == 0: def main (): print ("Welcome to Hangman V1.0!") print (" ") print ("--Main Menu--") print (" ") print ("1 - Start Hangman") print ("2 - Access File Menu") print ("3 - Quit the game") userchoice = int(input("Please select an option: ") if userchoice == ("3"): print ("Thank you and goodnight.") quit if userchoice == ("2"): displayFileMenu () if userchoice == ("1"): wordlistcheck HANGMAN = ( """ ------ | | | | | | | | | ---------- """, """ ------ | | | O | | | | | | ---------- """, """ ------ | | | O | -+- | | | | | ---------- """, """ ------ | | | O | /-+- | | | | | ---------- """, """ ------ | | | O | /-+-/ | | | | | ---------- """, """ ------ | | | O | /-+-/ | | | | | | ---------- """, """ ------ | | | O | /-+-/ | | | | | | | | | ---------- """, """ ------ | | | O | /-+-/ | | | | | | | | | | | ---------- """) MAX_WRONG = len(HANGMAN) - 1 WORDS = ("OVERUSED", "CLAM", "GUAM", "PUCK", "TAFFETA", "PYTHON") word = random.choice(WORDS) alreadyused = "-" * len(word) wrong = 0 used = [] print ("Welcome to Hangman. Good luck!") while (wrong < MAX_WRONG) and (alreadyused != word): print (HANGMAN[wrong]) print ("You've used the following letters:", used) print ("So far, the word is:", alreadyused) guess = input("Enter your guess: ") guess = guess.upper() while (guess in used): print ("Sorry. You've already guessed the letter:"), guess guess = input("Enter your guess: ") guess = guess.upper() used.append(guess) if (guess in word): print ("Correct,", guess, "is in the word!") new = "" for i in range(len(word)): if guess == word[i]: new += guess else: new += alreadyused[i] alreadyused = new else: print ("Sorry,", guess, "isn't in the word.") wrong += 1 if (wrong == MAX_WRONG): print (HANGMAN[wrong]) print ("You lose!") else: print ("You got it!") print ("The word was"), word main ()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question