【问题标题】:Hangman Game Issues with Printing _'s??? Logic Problem, I think打印_的刽子手游戏问题???逻辑问题,我觉得
【发布时间】:2019-02-17 20:39:53
【问题描述】:

这是我到目前为止编写的函数。

alreadyGuessed = []
def changeCharToUnderScore(eachLetter):
stringDisplay = ""
for x in eachLetter:
    stringDisplay += x
    stringDisplay += " "
    print (stringDisplay)
    print("Already guessed: " + ', '.join(alreadyGuessed))

当这打印我的hangman数组时,它会打印类似这样的内容

_)

_ _)

_ _ _)

_ _ _ _)

_ _ _ _ _)

代替打印

_ _ _ _ _) 我使用“)”是因为下划线本身会改变这里的格式

我已经修改了这个,我可以让它只打印一个,但它只打印第一个字母。 我知道我有某种逻辑错误,但我很难弄清楚我哪里出错了。

这是我目前的完整刽子手代码,但它正在进行中。

import time
from sys import stdout
import random as ran
wordList = ["potato", "tomato", "ramen", "moana", "disney", "veil", "space", "bowie", "russia", "chair", "couch", "glasses", "orange", "apple", "carrot", "bread", "head", "beer", "pasta", "soda", "pizza", "eggs", "noodle", "coffee", "soup", "feet", "hands", "ears", "hoodie", "pencil", "sorbet", "juice", "fan", "pan", "cup", "boba", "cheese", "chair", "purse", "knife", "spoon", "steak", "netflix", "lemon", "grape", "weed", "phone", "tire", "liar", "bench", "thirst"] # Dictionary
alreadyGuessed = [] # alpha characters already chosen
word = wordList[ran.randint(0, len(wordList) - 1)]
stringSplit = list(word) # Creates an array from the characters within the string.
stringInput = ["_"] * len(stringSplit)



def guessInput():
    printWordHint = ("The word is " + str(len(word)) + " letters long.\n")
     # Causes a delay inbetween each character being printed so that is creates the illusion of typing.
    for char in printWordHint:
        stdout.write(char)
        stdout.flush()
        time.sleep(0.03)
    return input("Guess a letter!")

### Have had issues getting this loop to print only the final array
# Prints the guessed characters, without the array brackets.
def changeCharToUnderScore(eachLetter):
    stringDisplay = ""
    for x in eachLetter:
        stringDisplay += x
        stringDisplay += " "
        print (stringDisplay)
        print("Already guessed: " + ', '.join(alreadyGuessed))

#replace hidden character from "_" to the "alpha character".
def replaceUnderScore(split, userInp):
    inputForGuess = guessInput()
    alreadyGuessed.append(inputForGuess)
    changedCharacter = True
    for x in range(len(split)):
        if inputForGuess == split[x]:
            userInp[x] = stringSplit[x]
            changedCharacter = False
    return(userInp, changedCharacter)

# Starting Game and calling Functions
changeCharToUnderScore(stringInput)
amtOfWrongGuesses = 0
correctGuess = True


while(not("_" not in stringInput or amtOfWrongGuesses >= 7)):
    stringInput, correctGuess = replaceUnderScore(stringSplit, stringInput)
    if not correctGuess:
        amtOfWrongGuesses += 1
        print("Oh no, that is not in this word!")
    changeCharToUnderScore(stringInput)
if(amtOfWrongGuesses >= 7):
    print("Blast Off! You LOSE! \nThe correct word was " + word)


else:
    print("Congratulations! You prevented the MoonMans death!")
    # winningString = ("Congratulations! You prevented the MoonMans death!")

如果他们想再玩一次,我也不确定如何让我的游戏循环播放。

【问题讨论】:

  • 您可以将数据格式化为代码,这样不会影响格式化。

标签: python-3.x function for-loop logic


【解决方案1】:

您应该在 for 循环之外打印结果字符串,当它完成连接字符串时:

def changeCharToUnderScore(eachLetter):
    stringDisplay = ""
    for x in eachLetter:
        stringDisplay += x
        stringDisplay += " "
    print (stringDisplay)
    print("Already guessed: " + ', '.join(alreadyGuessed))

【讨论】:

  • 谢谢!这完全奏效了。你和其他的建议真的很棒,现在我为了这么简单的事情而自责,哈哈。
【解决方案2】:
print (stringDisplay)

需要退出for循环。

如果您想允许游戏重播,您可以在游戏结束时嵌套另一个带有用户输入类型的 while 循环。

【讨论】:

    猜你喜欢
    • 2010-10-28
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    相关资源
    最近更新 更多