用Python编辑一个猜单词的程序,必须使用Function

用Python编辑一个猜单词的程序,必须使用Function
1.The second player has 7 guesses as there are 7 pieces to the hangman figure.The pieces are added to the figure in the following order:rope,head,torso,left arm,right arm,left leg,right leg.
2.At the beginning of the game the first player should be asked to enter the secret word.All characters are legal excluding and white space characters (tab,space,newline).After the user enters a word you should display 30 newlines so as to hide the secret word.
3.At the beginning of each round you should display in this order:The hanged man,the partially guessed secret word,the characters guessed so far.
4.For the secret word,the characters that have not been guessed should be displayed as
5.Guesses for the characters in the secret word should be case insensitive.Store the entered secret word in lowercase and convert all guessed characters to lower case.
6.Leading and trailing white space should be removed from the user's guess before checking it
7.The user must enter a guess.If they do not they should be told that they must enter a guess and receive the prompt to enter the next character again.This should continue until the user enters a new character.
8.If the user guesses a character that they have already guessed then they should be told that they already guessed that character and be given the opportunity to guess again.This should continue until the user enters a new character.These guesses should not be counted against the user.
9.The user can only guess 1 character at a time.If the user enters more than 1 character they should be told that they can only enter 1 character and be given the opportunity to guess again.This should continue until the user enters a new character.
10.The letters that have been guessed so far should be displayed in sorted order.
11.If the user guesses the secret word they should be told that they guessed the secret word.
12.If the user does not guess the secret word they should be told that they failed to guess the word and the word should be revealed.
13.You MUST USE FUNCTIONS for this program and you should approach this problem in a top down manner.Your program must have at least the following functions.The parameters your function accepts are up to you to decide.
1.get_secret_word
1.This function should get a valid secret word from the user and return it.
2.is_game_over
1.This function should return True if the game is over and false otherwise
3.display_hangman
1.This function should display the current hangman figure
4.display_guesses
1.This should display the guesses the user has made in alphabetical order
5.get_guess
1.This should get a valid guess from the user
静画林 1年前 已收到1个回答 举报

16392 花朵

共回答了18个问题采纳率:88.9% 举报

这英语看的头大,发你一个之前写的猜字游戏
#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.rn'
else:
print 'Sorry.rn'
i += 1
ret = myFind('*', ''.join(show))
if ret == False:
print "Congratulations. You correctly guessd the word: %srn" % ''.join(word)
break


def main(again):
if again == 'y':
word = setSecretWord()
print "
Word so far:***
Take guess number 1:e
Sorry.


Word so far:***
Take guess number 2:
.

1年前

10
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 16 q. 0.017 s. - webmaster@yulucn.com