猜猜词游戏:猜测秘密词不被接受
问题描述:
我在Python 3.x中工作,而且是相当新的,所以我希望我所问的是有道理的。我应该专注于这个猜字游戏的循环和字符串。 这里的(混乱/悠悠/杂乱)的代码,我到目前为止有:猜猜词游戏:猜测秘密词不被接受
import sys
import random
def Main():
PlayAgain = "y"
print("COP 1000 Project 4 - Courtney Kasonic - Guess the Word Game")
print("I'm thinking of a word; can you guess what it is?")
while PlayAgain == "y":
Words = "apple alphabet boomarang cat catharsis define decide elephant fish goat horizon igloo jackelope ketchup loop limousine monkey night octopus potato quick rebel separate test underway violin world yellow zebra".split()
SecretWord = random.choice(Words)
MissedLetters = ""
CorrectLetters = ""
ChosenWord = GetWord(Words)
Guess = FiveLetters(CorrectLetters+MissedLetters)
for Guess in ChosenWord:
CorrectLetters = CorrectLetters + Guess
ShowWord(CorrectLetters, ChosenWord)
for i in ChosenWord:
CLetters = ""
if Guess in ChosenWord:
Blanks = "_" * len(SecretWord)
for i in range(len(SecretWord)):
if SecretWord[i] in CLetters:
Blanks = Blanks[i] + SecretWord[i]
print(Blanks)
print(CLetters)
def GetWord(List):
SecretWord = random.choice(List)
return(SecretWord)
**def FiveLetters(LettersGuessed):
a = 2
if a > 1:
print("Enter five letters to check: ",end="")
Guess = input()
if len(Guess) != 5:
print("Please enter five letters.")
elif Guess in LettersGuessed:
print("You already guessed that letter.")
elif Guess not in "abcdefghijklmnopqrstuvwxyz":
print("Please enter a letter.")
else:
return(Guess)**
def ShowWord(CLetters, SecretWord):
print("\nHere is the word showing the letters that you guessed:\n")
CLetters = ""
Blanks = "_" * len(SecretWord)
for i in range(len(SecretWord)):
if SecretWord[i] in CLetters:
Blanks = Blanks[i] + SecretWord[i]
print(Blanks)
print(CLetters)
return(Blanks, SecretWord, CLetters)
def CheckLetters(Letters):
Letters = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split()
for Letters in Word:
print(Letters)
return(Letters)
Main()
加粗的区域是我有问题。只有五个字母可以输入“检查”,看看他们是否在秘密词。它只会接受像“abcde”这样的输入。它不会接受像“aaaaa”或“muihi”这样的输入,即它不会接受不合适的猜测或者有多个相同的字母。
我也遇到了下划线的问题。不知道我在那里的代码是否正确。正确猜测的字母不会替换相应的下划线。
例如:秘密字=狗。如果我猜字母“mopfe”(虽然我不能因为上面的问题),那么它会打印出“_ _ _”而不是“o”。
答
假设你不想失败,你将不可避免地提出更多的问题。如果你在*上提出这些问题,你应该可能是read the FAQ。既然你使用的是python,你应该也可以阅读样式指南(即PEP 8),它在评论中提到过。这段代码虽然不完整,但会让你开始。
import sys
import random
def main():
play_again = "y"
print("COP 1000 Project 4 - Courtney Kasonic - Guess the Word Game")
print("I'm thinking of a word; can you guess what it is?")
words = "apple alphabet boomarang cat catharsis define decide elephant fish goat horizon igloo jackelope ketchup loop limousine monkey night octopus potato quick rebel separate test underway violin world yellow zebra".split()
secret_word = random.choice(words)
correct_letters = ""
while play_again == "y":
guess = five_letters(correct_letters)
for letter in guess:
if letter in secret_word:
correct_letters += letter
show_word(correct_letters, secret_word)
def five_letters(letters_guessed):
guess = raw_input("Enter five letters to check: ")
if len(guess) != 5:
print("Please enter five letters.")
elif guess in letters_guessed:
print("You already guessed that letter.")
elif guess not in "abcdefghijklmnopqrstuvwxyz":
print("Please enter a letter.")
else:
return guess
def show_word(correct_letters, secret_word):
print("\nHere is the word showing the letters that you guessed:\n")
word_display = ""
for index, letter in enumerate(secret_word):
if letter in correct_letters:
word_display += letter
else:
word_display += "_"
print word_display
if __name__ == "__main__":
main()
请使用小写字母表示变量名称和函数。请使用大写字母 – jamylak 2013-03-22 02:03:09
是的,欢迎来到python,请去阅读pep8:http://www.python.org/dev/peps/pep-0008/ – monkut 2013-03-22 02:11:03
忽略代码所缺乏的“风格”,你可能需要自己调试您的代码,然后才能到其他人寻找您的错误。您应该逐个测试应用程序的每个部分,查找不适应的组件。这样做,你可能会自己解决问题,如果没有,那么你会孤立这个问题,并能够做出更多**简洁的问题,更多的用户愿意回答。 – asermax 2013-03-22 02:15:21