python-3.x - 打印_的刽子手游戏问题???逻辑问题,我想

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

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

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

当这打印我的刽子手数组时,它会打印出这样的东西

_ )

_ _)

_ _ _)

_ _ _ _)

_ _ _ _ _)

而不是打印

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

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

这是我目前的完整刽子手代码,但它正在进行中。
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!")

如果他们想再次玩,我也不知道如何让我的游戏循环。

最佳答案

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

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

关于python-3.x - 打印_的刽子手游戏问题???逻辑问题,我想,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52308154/

相关文章:

python-3.x - 如何将分组数据框多级索引转换为datadict

python - python循环导入是一个实现细节吗?

c - 基于CLRS教科书的归并排序

swift - 如何执行与时间相关的功能?

python - Python 中的类型及其可见性之间的差异

python - 动态 reshape Pandas 中的数据框

python - 有没有办法缩短多个条件,如下所示?

java - 循环问题- for : java

php - PHP 中 FOR 与 FOREACH 的性能

perl - 试图预测 future 的结果,collat​​z,Perl