我一直在尝试为我的计算机评估编写一个Hangman游戏,我遇到了一些障碍。
基本上,程序要求用户输入一个单词,然后运行一个循环来创建一个星号字符串,与输入单词的长度相同。
一旦用户输入了一个正确的字符,它将用正确的字符替换星号,但按顺序排列。例如,如果单词是“谎言”,并且用户输入“i”,则会将“ * ”更改为“ i ”。
代码如下。
def guess_part(word):
lives = 6
LetterCount = 0
LetterMask = ""
for x in range(len(word)):
LetterMask = LetterMask + "*"
print LetterMask
while lives != 0 and LetterMask.find("*")!=-1:
LetterGuess = raw_input("Enter a letter to guess?")
LetterCount = 0
for char in word:
LetterCount = LetterCount + 1
if LetterGuess == char:
print "Good Guess."
LetterMask = LetterMask.replace(LetterMask[LetterCount], LetterGuess)
print LetterMask
def rand_word():
from random import randrange #import the randrange function, from "random"
random_words = ['extraordinary','happy','computer','python','screen','cheese','cabaret','caravan','bee','wasp','insect','mitosis','electronegativity','jumper','trousers'] #list of different words which can be used by the program for the end user to guess.
word = random_words[randrange(0, 15)] #pick a random number, and use this number as an index for the list, "random_words".
guess_part(word) #call the function, "guess_part" with the parameter "word"
def user_word():
print "All words will be changed to lowercase."
print "Enter the word you would like to guess."
print ""
validation_input = False #Setting the validation unput to "False"
while validation_input == False: #while the validation input is not False, do below.
word = raw_input("") #Ask for input, and set the value to the variable, "word".
if word.isalpha(): #If word contains only strings, no numbers or symbols, do below.
word = word.lower() #set the string of variable, "word", to all lowercase letters.
guess_part(word) #call the function, "guess_part" with the parameter, "word".
validation_input = True #Break the while loop - set validation_input to "False".
else: #if the above isn't met, do the below.
print "Word either contained numbers or symbols."
def menu():
print "Hangman Game"
print ""
print "Ashley Collinge"
print ""
print "You will have 6 lives. Everytime you incorrectly guess a word, you will lose a life."
print "The score at the end of the game, is used to determine the winner."
print ""
print "Would you like to use a randomly generated word, or input your own?"
print "Enter 'R' for randomly generated word, or 'I' for your own input."
decision_bool = False #Set the decision_bool to "False".
decision_length = False #Set the decision_length to "False".
while decision_bool == False: #While decision_bool equals "False", do below.
decision = raw_input("") #Ask for input, value set to the variable "decision".
while decision_length == False: #While decision_length equals "False", do below.
if len(decision) == 1: #If the length of decision eqausl 1, do below.
decision_length = True #Set decision_length to "True."
decision = decision.capitalize() #Capitalize the string value of decision.
if decision == "R": #if the value of decision, eqauls "R".
print "You chose randomly generated word."
print ""
print "Forwarding..."
decision_bool = True #Set decision_bool to "True".
print ""
rand_word() #Call the function, rand_word()
elif decision =="I": #If decision equals "I", do below.
print "You chose to input your own word."
print ""
print "Forwarding..."
decision_bool = True #Set decision_bool to "False".
print ""
user_word() #Call the function, user_word()
else:
print "You entered an incorrect value for the question. Try again."
else:
print "You entered an incorrect value for the question. Try again."
menu()
我评论了大部分代码,但是如果有什么东西有点模糊,我会回答。
答案 0 :(得分:2)
我不打算为你写出你的整个程序,但简而言之:
假设word
是单词(例如word = 'liar'
)。然后我们需要一个函数将一个单词和一组猜到的字母翻译成一串已经猜到的星号+字母。
def asterisker(word, guesses=[]):
result = ""
for letter in word:
result += letter if letter in guesses else "*"
# which does what the below does:
# if letter in guesses:
# result += letter
# else:
# result += "*"
return result
给我们:
In [4]: asterisker("liar")
Out[4]: '****'
In [7]: asterisker("liar", ["l", "r" ])
Out[7]: 'l**r'
我可能会这样写,虽然上面的原文可能更好/更清楚。
def asterisker(word, guesses=[]):
return "".join(l if l in guesses else "*" for l in word)
编辑:另外,正如迈克所说(首先),如果有人做出错误的猜测,你需要递减“lives
”。
此外,以下是编写Python时可以使用的一些提示。
1)不要使用大写变量(如LetterMask
);如果您希望将其视为两个单词,请使用lettermask
或letter_mask
。
2)诸如“validation_input = False #Setting the validation unput to "False"
”之类的评论没有帮助,并且会使您的代码混乱。你将变量设置为False很明显,因为这正是代码所说的。如果您正在做的事情更加不清楚,评论可能会更有用。这(评论)实际上是编程中最困难的部分之一,而我仍然在努力解决这个问题。
3)你使用print ""
;如果你只是想打印一个换行符,你可以简单地使用print
(这将打印一个换行符),或者添加一个“\n
”(一个换行符;它非常酷)您要打印的字符串以打印换行符。试试看我的意思。
4)而不是像if something == False
那样测试布尔值,你可以简单地说if not something
,这更加清晰。同样,如果您正在测试if something == True
,则可以简单地说if something
。
5)在上面的解决方案中,我问自己“我想要得到什么”,而不是“我如何从我所处的位置到达我想去的地方”。差异是微妙的,你可能会说“艾萨克是个白痴”,我可能不会这么说,但这是一个重要的区别(我想!)
祝学习Python /编程好运!
答案 1 :(得分:1)
你很亲密,但并不完全。这里有一些提示:
1)您需要在lives
guess_part()
2)这个:
LetterMask = LetterMask.replace(LetterMask[LetterCount], LetterGuess)
没有按照你想要的方式工作。我会建议一些简单的事情,比如替换它:
LetterMask = list(LetterMask)
LetterMask[LetterCount-1] = LetterGuess
LetterMask = "".join(LetterMask)
3)另外注意(上面)字母计数中的“-1”,因为字符串是从0开始的,你是一个。
通过这些调整,你几乎都在那里。
答案 2 :(得分:0)
猜测问题出在guess_part()
函数上,这是一个有效的版本:
def guess_part(word):
lives = 6
# Make a mutable array of characters same length as word
LetterMask = bytearray("*" * len(word))
while lives > 0 and LetterMask != word:
print LetterMask
while True:
LetterGuess = raw_input("Enter a letter to guess: ")
if LetterGuess: break
LetterGuess = LetterGuess[0] # only take first char if more than one
if LetterGuess in LetterMask:
print "Sorry, you already guessed that letter. Try again."
countinue
GoodGuess = False
for i, char in enumerate(word):
if char == LetterGuess:
GoodGuess = True
LetterMask[i] = char
if GoodGuess:
print "Good guess."
else:
print "Sorry, bad guess"
lives -= 1
GuessedWholeWord = LetterMask == word
if GuessedWholeWord:
print "Congratulations, you guessed the whole word!"
else:
print "Sorry, no more guesses. You're hanged!"
return GuessedWholeWord