Jump to content
  • Advertisement
Sign in to follow this  

Python game - Hangman

This topic is 2920 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys

I am in my first year in university, and this is my second game program in python in 2 months of python.
I feel satisfied by this program (from what I've learnt and from my expectations)
But need some advice from all you guys.
How efficient is this program ?
Also, please advise oh how can I add a 'visual hangman' in this program.

Positive feedback is much appreciated :)


import random #for random.choice
guess=[] #null list
trial=int(0) #for keeping track of guessess
counter=int(0) #keeping track of position of element in list (if found)

for i in range(len(original)): #creating the '_ _**....' list
if (original=='A') or (original=='E') or (original=='I') or (original=='O') or (original=='U'):
guess.append("*") #* for vowels
guess.append("_")#_ for all other alphabets

print guess

while trial<9:
userinput=str.upper(raw_input('Input : '))

if len(userinput)>1: #checking for multiple characters
print 'Error : Input only a single character'

if userinput in original:

while userinput in temp: #loop for checking redundant characters


for i in range(0,len(temp)): #checking for final guess match with original
if temp=='_':

if counter==len(original): #if guess matches original
print 'Correct\t', guess
print 'You Win !'

print 'Correct\t' , guess , '\tTrials left: ', (9-trial)

print 'Incorrect', '\tTrials left: ', (9-trial)
print 'You Lose !'
print 'Correct answer was\t', original

Share this post

Link to post
Share on other sites
A tiny side note just to remember when coding in python - variables that are assigned integers do not need the integer type specified; Python defaults all integer references as regular integers, so you don't have to say 'variable = int(number)', you can just say 'variable = number'. If you add a decimal at the end of the number, it converts to a float.

Also, your question, "how efficient is this program" - very efficient. Could it be more efficient? Yes. Does it need to be? No. It's a very simple script and I'm pretty sure a 33 mHz computer with 8 mb of ram on Windows 95 would execute this program with an almost unnoticeable difference in performance than with a 6-core 3.4Ghz/16 GB ram machine.


Share this post

Link to post
Share on other sites
Lastly, if you really want to know where the slow points are, get a python profiler that profiles line-by-line (there probably is one, but don't quote me in that - I have only a little familiarity with Python).

Share this post

Link to post
Share on other sites
Thank you all for your replies !

Special thanks to Zahlman for that comprehensive reply !
Really helped a lot !

Please keep them coming ! :)

Share this post

Link to post
Share on other sites
I'm a programming newbie. But, I thought I'd add some non-programming advice.

When you start the program, you're confronted with some funny-looking symbols an a command line: "input." The user isn't even told they are playing hangman. You might say something like, print "Welcome to Hangman! Guess a letter. You have X wrong guesses. If you guess all the letters in the word before your guesses run out, you win!"

Also, were you looking for ASCII art? Here is a very simple hangman (the white space will display incorrectly):

|/ |
| (_)
| /|\
| |
| / \

When we were kids, you had seven guesses, one for each letter of hangman. We'd draw the rope/noose, head, body, left arm, right arm, left leg, right leg and write each letter of hangman under the gallows for each wrong guess.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!