【问题标题】:Guess the Word Game: Guesses For Secret Word Aren't Being Accepted猜词游戏:不接受对秘密词的猜测
【发布时间】:2013-03-22 10:27:16
【问题描述】:

我在 Python 3.x 中工作并且是相当新的,所以我希望我的要求是有意义的。我应该专注于这个猜词游戏的 for 循环和字符串。 这是我到目前为止的(混乱/冗长/杂乱)代码:

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”的“_ _ _”。

【问题讨论】:

  • 请使用小写的变量名和函数。类使用大写字母
  • 是的,欢迎来到python,请去阅读pep8:python.org/dev/peps/pep-0008
  • 忽略你的代码缺乏“风格”,你可能需要自己调试你的代码,然后才能让其他人来寻找你的错误。您应该单独测试应用程序的每个部分,寻找行为不正常的组件。这样做,您可能能够自己解决问题,如果不能,那么您将隔离问题并能够提出更多用户愿意的更简洁的问题回答。

标签: python


【解决方案1】:

假设您不想失败,您将不可避免地提出更多问题。如果你在 StackOverflow 上问这些问题,你可能应该read the FAQ。由于您使用的是 python,因此您可能还应该阅读样式指南(即 PEP 8),它在 cmets 中有所提及。此代码虽然不完整,但可以帮助您入门。

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    相关资源
    最近更新 更多