2010年6月28日 星期一

[Question 8] Hangman version2


Learn something :

1. If yes then draw a line next to block,otherwise draw under block.
2. Using Flow Chart to design code is more convenience.



Flow Chart :





main code :

#! /usr/bin/env python
import test_1

print 'Welcome to hangman. You get seven chances to guess the mystery word.'

a='happy'
m=list(a)

# draw the dash line
i=0
while i print '_',
i+=1

# This is main function

test_1.guess(m)

# a='happy birthday'
# m=list(a)
# print m
# Result : ['h', 'a', 'p', 'p', 'y', ' ', 'b', 'i', 'r', 't', 'h', 'd', 'a', 'y']



test_1.py :

#! /usr/bin/env python
def draw(n) :
if n == 1 :
print ' 0'
elif n == 2 :
print ' 0'
print ' |'
elif n == 3 :
print ' 0'
print '\|'
elif n == 4 :
print ' 0'
print '\|/'
elif n == 5 :
print ' 0'
print '\|/'
print '/'
elif n == 6 :
print ' 0'
print '\|/'
print '/ \.'

def guess(orignal_word):
array=[]
array_history=[]
k=0 # calculate about how many letter do you got. When you guess right.
j=0 # calculate about how many letter do you got. When you guess wrong.
while 1 :
input_word = raw_input('\n\nPick a letter -->')
if input_word in '1234567890' :
flag=1 # when flag = 1 will not draw the picture.
print input_word + ' is not a valid letter'
elif len(input_word) > 2 :
flag=1 # when flag = 1 will not draw the picture.
print input_word + ' has more than one letter.'
elif len(array)==0 :
flag = 0
array_history.append(input_word)
i=0 # How many loop we must to do.
while i if orignal_word[i] == input_word:
flag=1 # when flag = 1 will not draw the picture.
array.insert(i,input_word)
k+=1
i+=1
else :
#print '_',
array.insert(i, '_')
i+=1
else :
flag=0
array_history.append(input_word)
i=0 # How many loop we must to do.
while i if orignal_word[i] == input_word:
flag = 1 # flag = 1 do not draw picture.
if array[i]!=input_word:
array.pop(i)
array.insert(i,input_word)
k+=1 # calculate about how many letter do you got. When you guess right.
i+=1
else :
i+=1

else :
i+=1



if flag != 1 :
j+=1
draw(j)

print 'Guessed letters:'+''.join(array_history).upper()
print ' '.join(array)


if j > 5 : # guess wrong 6 times.
orignal_word = ''.join(orignal_word)
print 'So sorry. You struck out.'
print 'The mystery word was '+orignal_word +'.\n\n'
break

if k == len(orignal_word) :
print 'Congratulation, you win this game'
break


Related Posts:

0 意見:

張貼留言