python练习,不能使用find函数,命令中至少构建3个函数,要求python2.7版本,希望尽
#coding=utf-8
'''
Created on 2014-11-03
@author: Neo
'''
def myFind(key, word):
flag = False
ret = []
for i in range(0, word.__len__()):
if key == word[i]:
flag = True
ret.append(i)
if flag:
return ret
else:
return flag
def setSecretWord():
word = raw_input("Enter the secret word(all in lowercase):")
ret = []
for i in range(0, word.__len__()):
ret.append(word[i])
return ret
def showWord(word):
show = ['*']*word.__len__()
i = 1
while True:
print "Word so far: %s" % ''.join(show)
key = raw_input("Take guess number %d:" % i)
flag = myFind(key, word)
if flag:
for k in range(0,flag.__len__()):
show[flag[k]] = key
print 'Got it.\r\n'
else:
print 'Sorry.\r\n'
i += 1
ret = myFind('*', ''.join(show))
if ret == False:
print "Congratulations. You correctly guessd the word: %s\r\n" % ''.join(word)
break
def main(again):
if again == 'y':
word = setSecretWord()
print "============================================"
showWord(word)
again = raw_input("Do you want to play one more time?y/n?")
main(again)
if __name__ == "__main__":
main('y')
运行结果:
Enter the secret word(all in lowercase):hello
============================================
Word so far: *****
Take guess number 1:e
Got it.
Word so far: *e***
Take guess number 2:s
Sorry.
Word so far: *e***
Take guess number 3:l
Got it.
Word so far: *ell*
Take guess number 4:h
Got it.
Word so far: hell*
Take guess number 5:o
Got it.
Congratulations. You correctly guessd the word: hello
Do you want to play one more time?y/n?y
Enter the secret word(all in lowercase):two
============================================
Word so far: ***
Take guess number 1:e
Sorry.
Word so far: ***
Take guess number 2:
..................
十分感谢
猜单词的一个游戏
2014-11-03
广告 您可能关注的内容 |