Posted by: Sourav | January 6, 2017

Hangman Game using Python 2.7,Python Teacher Sourav,Kolkata 09748184075


import random
import sys
HANGMANPICS = [”’

+—+
| |
|
|
|
|
=========”’, ”’

+—+
| |
O |
|
|
|
=========”’, ”’

+—+
| |
O |
| |
|
|
=========”’, ”’

+—+
| |
O |
/| |
|
|
=========”’, ”’

+—+
| |
O |
/|\ |
|
|
=========”’, ”’

+—+
| |
O |
/|\ |
/ |
|
=========”’, ”’

+—+
| |
O |
/|\ |
/ \ |
|
=========”’]
words=(‘america’,’indians’,’iraqians’,’solaris’,’sunflower’,’srilanka’)

word=random.choice(words)
#print ‘_ ‘*len(word)
user_effort=’_’*len(word)
print ‘the random word was’

for i in range(0,len(word)):
if (i%2)==0:
sys.stdout.write(word[i])
else:
sys.stdout.write(‘_’)

print

i=0
j=0
count=0
while i<len(word):
count+=1

char=raw_input(‘enter character’)
if char==word[i]:
user_effort=user_effort[:i]+char+user_effort[i+1:]
print user_effort

i+=1
else:
print user_effort
print HANGMANPICS[j]
if j<=(len(HANGMANPICS)-2):
j+=1
else:
print ‘sorry you could not save the man and the word was ‘,word
break

if user_effort==word:
print ‘congrats you have guessed the word in ‘,count,’ chances’
break

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: