这是我到目前为止编写的函数。
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!")
如果他们想再次玩游戏,我也不确定如何让我的游戏循环播放。
答案 0 :(得分:2)
当完成字符串的串联后,应在for
循环外部打印结果字符串:
def changeCharToUnderScore(eachLetter):
stringDisplay = ""
for x in eachLetter:
stringDisplay += x
stringDisplay += " "
print (stringDisplay)
print("Already guessed: " + ', '.join(alreadyGuessed))
答案 1 :(得分:2)
print (stringDisplay)
需要从for循环中退出。
如果要允许重播游戏,则可以在游戏结束时嵌套另一while循环,并提供一种用户输入。