我正在使用for循环,它需要我打印一些东西。每次打印我都不希望输出在单独的行上。我该怎么做?
def getGuessedWord(secretWord, lettersGuessed):
for i in secretWord:
if (i in lettersGuessed):
print(i)
else:
print ("_")
此代码适用于刽子手游戏 提前致谢
答案 0 :(得分:1)
您可以通过添加
来阻止在单独的行上打印, end=""
在print()函数的末尾,如:
def getGuessedWord(secretWord, lettersGuessed):
for i in secretWord:
if (i in lettersGuessed):
print(i, end="") # end
else:
print ("_", end="") # end
print()
# Test
getGuessedWord("HELLO",['A','B','E','H'])
返回:
HE___