Can anyone please show me their hangman code in Python.

11 views
Skip to first unread message

Uttam Shrestha

unread,
Aug 21, 2010, 8:11:11 AM8/21/10
to FOSS Nepal Python Training Group
Can anyone please show me their hangman code in Python.

Jwalanta Shrestha

unread,
Aug 21, 2010, 8:26:07 AM8/21/10
to pyfo...@googlegroups.com
On Sat, Aug 21, 2010 at 5:56 PM, Uttam Shrestha <utt...@gmail.com> wrote:
Can anyone please show me their hangman code in Python.

^ kindly do not ask us to do your homework :)
~jwalanta

Pravin Raj Joshi

unread,
Aug 21, 2010, 9:17:43 PM8/21/10
to pyfo...@googlegroups.com
I agree with Jwalanta on not doing your assignment.
But guidance and a little bit of help is always welcome.
So attached is the flowchart of hangman game.
Below is the code that will get a secret word and meaning.

import random

wordDict = { 'orange' : 'fruit for winter',
'apple' : 'fruit that keeps the doctor away',
'momo' : 'Nepal\' national food',
'sekuwa' : 'drunken delights',
'boost' : 'secret of my energy',
'rain' : 'falls from the sky'}

randomSelection = random.randint(0,len(wordDict)-1)
selectedWord = wordDict.keys()[randomSelection]
selectedMeaning = wordDict[selectedWord]

noOfLettersInWord = len(selectedWord)
print selectedWord, selectedMeaning
print "__ " * noOfLettersInWord

Hope that helps.

Pravin

HMalgorithm.jpg

Uttam Shrestha

unread,
Aug 21, 2010, 10:45:17 PM8/21/10
to FOSS Nepal Python Training Group
thanks! It helps a lot! Bt still sometimes i feel, its better 2
understand than to just finish the assignment.

Can anyone plz tell me more about the functions in details:

random.randint(0,len(wordDict)-1)

selectedWord = wordDict.keys()[randomSelection]

selectedMeaning = wordDict[selectedWord]
noOfLettersInWord = len(selectedWord)

print selectedWord, selectedMeaning
print "__ " * noOfLettersInWord

I m tryin 2 catchup things by downloading few ebooks.

Pravin Raj Joshi

unread,
Aug 22, 2010, 12:45:33 AM8/22/10
to pyfo...@googlegroups.com
#generates a random integer between 0 and the length of word dictionary
#This is done to randomly select a word from the word dictionary
randomSelection = random.randint(0,len(wordDict)-1)

#this selects the word that is indexed by the random number generated above
selectedWord = wordDict.keys()[randomSelection]

#this gives the meaning of the word
#the word and meaning are arranged in the dictionary as a key - value pair
#where key is the word and value is the meaning of the word
#the meaning is utilized to provide hint in the game
selectedMeaning = wordDict[selectedWord]

#gets the number of characters in the word
noOfLettersInWord = len(selectedWord)

#just prints the selected word and its meaning
print selectedWord, selectedMeaning

#prints blanks to enter the word. The number of blanks equals the number
of letters in the randomly selected word.
print "__ " * noOfLettersInWord

I hope that helps.

Pravin

Reply all
Reply to author
Forward
0 new messages