Sign in to follow this  
techguitar

Python game - Hangman

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 :)

[code]

#hangman

import random #for random.choice
dictionary=['COMPUTER', 'LAPTOP', 'IDEAS', 'NANYANG', 'COLLEGE']
word=random.choice(dictionary)
original=list(word)
temp=list(word)
guess=[] #null list
trial=int(0) #for keeping track of guessess
userinput=''
counter=int(0) #keeping track of position of element in list (if found)

for i in range(len(original)): #creating the '_ _**....' list
if (original[i]=='A') or (original[i]=='E') or (original[i]=='I') or (original[i]=='O') or (original[i]=='U'):
guess.append("*") #* for vowels
else:
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'
continue

if userinput in original:

while userinput in temp: #loop for checking redundant characters
counter=temp.index(userinput)
guess[counter]=userinput
temp.remove(userinput)
temp.insert(counter,'_')

counter=0

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

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

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

else:
trial+=1
print 'Incorrect', '\tTrials left: ', (9-trial)
else:
print 'You Lose !'
print 'Correct answer was\t', original
[/code]

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.

-Adam

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):

[code]
SECRET WORD
_________
|/ |
| (_)
| /|\
| |
| / \
|
|___
HANGMAN
[/code]

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

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

Sign in to follow this