-
-
Save DevDarren/4199441 to your computer and use it in GitHub Desktop.
class Hangman(): | |
def __init__(self): | |
print "Welcome to 'Hangman', are you ready to die?" | |
print "(1)Yes, for I am already dead.\n(2)No, get me outta here!" | |
user_choice_1 = raw_input("->") | |
if user_choice_1 == '1': | |
print "Loading nooses, murderers, rapists, thiefs, lunatics..." | |
self.start_game() | |
elif user_choice_1 == '2': | |
print "Bye bye now..." | |
exit() | |
else: | |
print "I'm sorry, I'm hard of hearing, could you repeat that?" | |
self.__init__() | |
def start_game(self): | |
print "A crowd begins to gather, they can't wait to see some real" | |
print "justice. There's just one thing, you aren't a real criminal." | |
print "No, no. You're the wrong time, wrong place type. You may think" | |
print "you're dead, but it's not like that at all. Yes, yes. You've" | |
print "got a chance to live. All you've gotta do is guess the right" | |
print "words and you can live to see another day. But don't get so" | |
print "happy yet. If you make 6 wrong guess, YOU'RE TOAST! VAMANOS!" | |
self.core_game() | |
def core_game(self): | |
guesses = 0 | |
letters_used = "" | |
the_word = "pizza" | |
progress = ["?", "?", "?", "?", "?"] | |
while guesses < 6: | |
guess = raw_input("Guess a letter ->") | |
if guess in the_word and not in letters_used: | |
print "As it turns out, your guess was RIGHT!" | |
letters_used += "," + guess | |
self.hangman_graphic(guesses) | |
print "Progress: " + self.progress_updater(guess, the_word, progress) | |
print "Letter used: " + letters_used | |
elif guess not in the_word and not(in letters_used): | |
guesses += 1 | |
print "Things aren't looking so good, that guess was WRONG!" | |
print "Oh man, that crowd is getting happy, I thought you" | |
print "wanted to make them mad?" | |
letters_used += "," + guess | |
self.hangman_graphic(guesses) | |
print "Progress: " + "".join(progress) | |
print "Letter used: " + letters_used | |
else: | |
print "That's the wrong letter, you wanna be out here all day?" | |
print "Try again!" | |
def hangman_graphic(self, guesses): | |
if guesses == 0: | |
print "________ " | |
print "| | " | |
print "| " | |
print "| " | |
print "| " | |
print "| " | |
elif guesses == 1: | |
print "________ " | |
print "| | " | |
print "| 0 " | |
print "| " | |
print "| " | |
print "| " | |
elif guesses == 2: | |
print "________ " | |
print "| | " | |
print "| 0 " | |
print "| / " | |
print "| " | |
print "| " | |
elif guesses == 3: | |
print "________ " | |
print "| | " | |
print "| 0 " | |
print "| /| " | |
print "| " | |
print "| " | |
elif guesses == 4: | |
print "________ " | |
print "| | " | |
print "| 0 " | |
print "| /|\ " | |
print "| " | |
print "| " | |
elif guesses == 5: | |
print "________ " | |
print "| | " | |
print "| 0 " | |
print "| /|\ " | |
print "| / " | |
print "| " | |
else: | |
print "________ " | |
print "| | " | |
print "| 0 " | |
print "| /|\ " | |
print "| / \ " | |
print "| " | |
print "The noose tightens around your neck, and you feel the" | |
print "sudden urge to urinate." | |
print "GAME OVER!" | |
self.__init__() | |
def progress_updater(self, guess, the_word, progress): | |
i = 0 | |
while i < len(the_word): | |
if guess == the_word[i]: | |
progress[i] = guess | |
i += 1 | |
else: | |
i += 1 | |
return "".join(progress) | |
game = Hangman() | |
I tried running this code and gave me a syntax error...
File "hangman_1.py", line 36
if guess in the_word and not in letters_used:
^
SyntaxError: invalid syntax
The caret was displayed under the second "in" in that line. Any thoughts? Thanks!
@Kalaborative You need to alte the code to
if guess in the_word and not guess in letters_used:
the same error appears in a few other places as well
Greetings Prof
when i tried the code it was executing but then the loop aint ending after finding out the word
Any thoughts? Thanks!
The expression is not complete:
if guess in the_word and guess not in letters_used:
that should fix it
I have found error in line 115. Please help
Also in line 8, 23, 42 error is shown.
change line no. 36 by following
if guess in the_word and not letters_used:
change line no. 42 by following
elif guess not in the_word and not(letters_used):
the code runs but loop not ending
Excellent code! Just one insignificant change: guess the right letters <-- in line 23
here's my version, errors are corrected:
import time
class Hangman():
def init(self):
print "Welcome to 'Hangman game, you need to guess the word or else ..."
print "you will be hanged"
print "press enter to continue"
raw_input("->")
self.start_game()
def start_game(self):
print "Starting game..."
time.sleep(1)
self.core_game()
def core_game(self):
guesses = 0
letters_used = ""
the_word = "pizza"
progress = ['*','*','*','*','*']
while guesses < 6:
guess = raw_input("Guess a letter ->")
if (guess in the_word) and (guess not in letters_used):
print "Your guess was RIGHT!"
letters_used += guess +","
self.hangman_graphic(guesses)
print "Progress: " + self.progress_updater(guess, the_word, progress)
print "Letter used: " + letters_used
elif (guess not in the_word) and (guess not in letters_used):
guesses += 1
print "Wrong Guess!"
letters_used += guess + ","
self.hangman_graphic(guesses)
print "Progress: " + "".join(progress)
print "Letter used: " + letters_used
else:
print "You already typed this letter"
print "Try again!"
if ''.join(progress) == the_word:
print "You Guessed the word:" + the_word
print "You Won"
break
def hangman_graphic(self, guesses):
if guesses == 0:
print "________ "
print "| | "
print "| "
print "| "
print "| "
print "| "
elif guesses == 1:
print "________ "
print "| | "
print "| 0 "
print "| "
print "| "
print "| "
elif guesses == 2:
print "________ "
print "| | "
print "| 0 "
print "| / "
print "| "
print "| "
elif guesses == 3:
print "________ "
print "| | "
print "| 0 "
print "| /| "
print "| "
print "| "
elif guesses == 4:
print "________ "
print "| | "
print "| 0 "
print "| /|\ "
print "| "
print "| "
elif guesses == 5:
print "________ "
print "| | "
print "| 0 "
print "| /|\ "
print "| / "
print "| "
else:
print "________ "
print "| | "
print "| 0 "
print "| /|\ "
print "| / \ "
print "| "
print"YOU LOST"
print "GAME OVER!"
self.__init__()
def progress_updater(self, guess, the_word, progress):
i = 0
while i < len(the_word):
if guess == the_word[i]:
progress[i] = guess
i += 1
else:
i += 1
return "".join(progress)
game = Hangman()
yo itz ya boy chicken :)
That is a very fun game, except "Pizza" has two z's and would'nt let me guess the other z, bummer, i am working on same thing
hi there the whole code is saying that its messed up plz could some one help
the code is error please send here the correct code
@PoojaAmolDeshmukh please send the correct code
Implemented the game in a little simple way but without graphics !
A python 3 code.
check it out at : https://github.com/souravrane/Hangman
Is that a full code? because when i run the corrected code above, it gives no output
nice