Jump to content

how to code hangman game in python?

hey guys need help in coding hangman game in python . i have it as an assignment and im completely lost dont know where to start from!

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/244214-how-to-code-hangman-game-in-python/
Share on other sites

Link to post
Share on other sites

hey guys need help in coding hangman game in python . i have it as an assignment and im completely lost dont know where to start from!

When is it in for? 

 

Have a google around to get your bearings. 

"Use the force Harry" 

                   -Gandalf

Link to post
Share on other sites

Do you have the instructions for the assignment? You'll need to post them here for us to understand what the assignment wants. I'll need more details than just coding a hangman game :unsure: .

Quote

The problem is that this is an nVidia product and scoring any nVidia product a "zero" is also highly predictive of the number of nVidia products the reviewer will receive for review in the future.

On 2015-01-28 at 5:24 PM, Victorious Secret said:

Only yours, you don't shitpost on the same level that we can, mainly because this thread is finally dead and should be locked.

On 2016-06-07 at 11:25 PM, patrickjp93 said:

I wasn't wrong. It's extremely rare that I am. I provided sources as well. Different devs can disagree. Further, we now have confirmed discrepancy from Twitter about he use of the pre-release 1080 driver in AMD's demo despite the release 1080 driver having been out a week prior.

On 2016-09-10 at 4:32 PM, Hikaru12 said:

You apparently haven't seen his responses to questions on YouTube. He is very condescending and aggressive in his comments with which there is little justification. He acts totally different in his videos. I don't necessarily care for this content style and there is nothing really unique about him or his channel. His endless dick jokes and toilet humor are annoying as well.

 

 

Link to post
Share on other sites

Simulate “Hangman” game played by two players. For those who have never played the game, the

rules are simple. One player thinks of a secret word, and the other tries to guess it, letter by letter.

To learn more about “hangman” game, check out this website: http://www.manythings.org/hmf/

For this question, you don’t have to worry about creating user interface. Thus, our Hangman game

is not a “real” Hangman game, but the idea is similar.

 

 

this is the question ! oh and yes im paying attention in the class but programming is not easy!

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to post
Share on other sites

Pay attention in class ;)

iam man programming is not easy!

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to post
Share on other sites

When is it in for? 

 

Have a google around to get your bearings. 

i have untill wed but i have a midterm and a essay to write in the same week! :( 

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to post
Share on other sites

i have untill wed but i have a midterm and a essay to write in the same week! :(

 

This may help.. 

 

http://www.pythonforbeginners.com/code-snippets-source-code/game-hangman

 

Does your uni not have plagerism detection software?

"Use the force Harry" 

                   -Gandalf

Link to post
Share on other sites

This may help.. 

 

http://www.pythonforbeginners.com/code-snippets-source-code/game-hangman

 

Does your uni not have plagerism detection software?

i actually found it but i am not suppose to use break function and i am not suppose to import time! so its completely lost!

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to post
Share on other sites

i actually found it but i am not suppose to use break function and i am not suppose to import time! so its completely lost!

Have a look around, Hangman is quite a common task in programming courses there are solutions around. 

 

Don't you have any help from your notes? 

"Use the force Harry" 

                   -Gandalf

Link to post
Share on other sites

Have a look around, Hangman is quite a common task in programming courses there are solutions around. 

 

Don't you have any help from your notes? 

nope :( i have to figure it out myself no notes for such kind of tasks!

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to post
Share on other sites

def Hangman():    word = raw_input("enter the secret word(all in lowercase):")    turns = len(word)+5    guess = raw_input("enter your guess:")    while turns > 0:       for char in word:           if guess == char:               return char,           else:               return "_"       turns -= 1                  

guys this is so far my code for hangman and it is not working! :( 

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to post
Share on other sites

first you have to piece together everything you want the program to do before writing any code: pseudo code

 

eg.

  1. enter word
  2. guess a letter
  3. if the letter is part of the word reveal it
  4. if it isnt add a body part
  5. keep doing these steps until person is either dead or word is guessed
Link to post
Share on other sites

 

first you have to piece together everything you want the program to do before writing any code: pseudo code

 

eg.

  1. enter word
  2. guess a letter
  3. if the letter is part of the word reveal it
  4. if it isnt add a body part
  5. keep doing these steps until person is either dead or word is guessed

 

hey man i have the same  pseudo code but implementation is very difficult and i am multitasking reading for my essay and programming at the same time! it is becoming a hell of a lot harder! 

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to post
Share on other sites

hey man i have the same  pseudo code but implementation is very difficult and i am multitasking reading for my essay and programming at the same time! it is becoming a hell of a lot harder! 

while (!done) {    cout << "Guess another letter: ";    cin << letter;    for (int i =0; i < word.length; i++) {        if (letter == charAt(i)) {            // show letter        } else {            // lower their score or add body part        }    }}

Soemthing like this for the basic skeleton but I did in c++. cout and cin are just input and outputs btw

Link to post
Share on other sites

hey man i have the same  pseudo code but implementation is very difficult and i am multitasking reading for my essay and programming at the same time! it is becoming a hell of a lot harder! 

Show us your progress so far, Telling you the answer straight up is not going to help you at all, making sure you can join the dots will. What exactly are you stuck on? 

 

When you have that eurka moment, oh this goes together like this and this can do this and therefore I can do this, that is more beneficial to you than, here's the answer. 

"Use the force Harry" 

                   -Gandalf

Link to post
Share on other sites

Show us your progress so far, Telling you the answer straight up is not going to help you at all, making sure you can join the dots will. What exactly are you stuck on? 

 

When you have that eurka moment, oh this goes together like this and this can do this and therefore I can do this, that is more beneficial to you than, here's the answer. 

so far im working on hangman code! but my teacher is asking me to do it in this way 

word = raw_input("enter the secret word")guesses = ''turns = len(word)+5while turns > 0:    failed = 0    for char in word:        if char in guesses:            print char        else:            failed += 1    if failed == 0 :        print "you have won"    guess = raw_input("guess a char: ")    guesses += guess    if guess not in word:        turns -= 1        print "sorry"        if turn == 0:            print"you loose"

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to post
Share on other sites

You're getting really close. I was able to play a full game though.

thanks man but it is no good to be honest , my teacher want it a different format. i will pm you the thing

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to post
Share on other sites

thanks man but it is no good to be honest , my teacher want it a different format. i will pm you the thing

 

Yeah, it still needs some work but you're making progress.

 

I've copied the required program output from his PM and attached his assignment pdf if anyone wants to take a look at it.

 

 

Game play should look like this (The word/characters in bold face are user input):
 

 

Enter the secret word (all in lowercase): hello
===============================
 
Word so far: *****
Take guess number 1: e
Got it!
 
Word so far: *e***
Take guess number 2: s
Sorry.
 
Word so far:*e***
Take guess number 3: l
Got it!
 
Word so far: *ell*
Take guess number 4: h
Got it!
 
Word so far: hell*
Take guess number 5: o
Got it!
 
Congratulations. You correctly guessed the word: hello
 
Do you want to play one more time? y/n? y
 
Enter the secret word (all in lowercase): two
===============================
 
Word so far: ***
Take guess number 1: e
...

Assignment_2.pdf

Link to post
Share on other sites

Remember to follow the coding requirements. Right now you don't have any functions and you need at least 3.

 

Here's an easy way to get 2 functions along with some hints on how to repeat multiple games.

def hangman():    # Most of your games code (ie: what you've already got done)def calculate_number_of_guesses(word):    # turn requirement 'a' into a function    # ie: Calculate the number of guesses allowed based on the    # secret word passed in to the functionif __name__ == '__main__':    # In a loop    # Start the game    # When it is done ask the user if they want to continue    # Handle choice appropriately

You'll be able to get more functions from the code in the hangman function.

 

Note, you should probably only use a main function if __name__ == '__main__': if you've learned about it in class.

Link to post
Share on other sites

Remember to follow the coding requirements. Right now you don't have any functions and you need at least 3.

 

Here's an easy way to get 2 functions along with some hints on how to repeat multiple games.

def hangman():    # Most of your games code (ie: what you've already got done)def calculate_number_of_guesses(word):    # turn requirement 'a' into a function    # ie: Calculate the number of guesses allowed based on the    # secret word passed in to the functionif __name__ == '__main__':    # In a loop    # Start the game    # When it is done ask the user if they want to continue    # Handle choice appropriately

You'll be able to get more functions from the code in the hangman function.

 

Note, you should probably only use a main function if __name__ == '__main__': if you've learned about it in class.

Thanks for the heads up but I just have 3hrs until submission I think I will have this submit the present code :( anyways thanks a lot for the help ! Ibrealky appreciate it 

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to post
Share on other sites

Thanks for the heads up but I just have 3hrs until submission I think I will have this submit the present code :( anyways thanks a lot for the help ! Ibrealky appreciate it 

That's enough time to at least get some more marks even if you can't fully finish it. But it's up to you. Good luck.

Link to post
Share on other sites

Also teachers really love it when you document your code. good luck

 

It's also one of the requirements

 

e) Your code should be easy to read and understand. This includes (but isn’t limited to) good variable/function names, code formatting, comments, and docstrings. Place all your functions before the top-level code. The top-level code cannot be one single function call. General structure and logic of your program will also be marked.

 

Should be at least a couple of easy marks there.

Link to post
Share on other sites

Here is the full code below, i have taught this code before and would expect something along these lines... Just copy the code below and make sure you alter some of the words so they relate to your education establishment. 

 

Copy the code below:

 

# Hangman Game

#

# The classic game of Hangman.  The computer picks a random word

# and the player wrong to guess it, one letter at a time.  If the player

# can't guess the word in time, the little stick figure gets hanged.

 

# imports

import random

 

# constants

HANGMAN = (

"""

 ------

 |    |

 |

 |

 |

 |

 |

 |

 |

----------

""",

"""

 ------

 |    |

 |    O

 |

 |

 |

 |

 |

 |

----------

""",

"""

 ------

 |    |

 |    O

 |   -+-

 | 

 |   

 |   

 |   

 |   

----------

""",

"""

 ------

 |    |

 |    O

 |  /-+-

 |   

 |   

 |   

 |   

 |   

----------

""",

"""

 ------

 |    |

 |    O

 |  /-+-/

 |   

 |   

 |   

 |   

 |   

----------

""",

"""

 ------

 |    |

 |    O

 |  /-+-/

 |    |

 |   

 |   

 |   

 |   

----------

""",

"""

 ------

 |    |

 |    O

 |  /-+-/

 |    |

 |    |

 |   | 

 |   | 

 |   

----------

""",

"""

 ------

 |    |

 |    O

 |  /-+-/

 |    |

 |    |

 |   | |

 |   | |

 |  

----------

""")

 

MAX_WRONG = len(HANGMAN) - 1

WORDS = ("PYTHON", "COMPUTING", "ICT", "PROGRAMMING", "HARDWARE", "SOFTWARE", "MONITOR", "KEYBOARD", "MOUSE", "CD", "HARDDRIVE", "PENDRIVE", "FILE", "INTERNET", "CPU", "GPU", "RAM", "BYTE", "ROM", "GUI", "LAN", "DOS", "VIRUS", "BITMAP", "VECTOR", "BROSWER", "CLOCK SPEED", "COOKIE", "DATABASE", "WORD PROCESSING", "EXCEL", "PUBLISHER", "MICROSOFT", "WINDOWS", "FIREWALL", "GIF", "HTML", "BIOS", "INPUT", "OUTPUT")

 

# initialize variables

word = random.choice(WORDS)   # the word to be guessed

so_far = "-" * len(word)      # one dash for each letter in word to be guessed

wrong = 0                     # number of wrong guesses player has made

used = []                     # letters already guessed

 

 

print("Welcome to Hangman.  Good luck!")

 

while wrong < MAX_WRONG and so_far != word:

    print(HANGMAN[wrong])

    print("\nYou've used the following letters:\n", used)

    print("\nSo far, the word is:\n", so_far)

 

    guess = input("\n\nEnter your guess: ")

    guess = guess.upper()

    

    while guess in used:

        print("You've already guessed the letter", guess)

        guess = input("Enter your guess: ")

        guess = guess.upper()

 

    used.append(guess)

 

    if guess in word:

        print("\nYes!", guess, "is in the word!")

 

        # create a new so_far to include guess

        new = ""

        for i in range(len(word)):

            if guess == word:

                new += guess

            else:

                new += so_far              

        so_far = new

 

    else:

        print("\nSorry,", guess, "isn't in the word.")

        wrong += 1

 

if wrong == MAX_WRONG:

    print(HANGMAN[wrong])

    print("\nYou've been hanged!")

else:

    print("\n\n\nYou guessed it!",

          """

 ------

 |       IM FREE!!!

 |

 |    O

 |  /-+-/

 |    |

 |    |

 |   | |

 |   | |

----------

""")

            

print("\nThe word was", word)

 

input("\n\nPress the enter key to exit.")

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×