Reputation: 79
I am using a for loop and it requires me to print something. Each time it prints I dont want the output to be on a seperate line. How do I do this?
def getGuessedWord(secretWord, lettersGuessed):
for i in secretWord:
if (i in lettersGuessed):
print(i)
else:
print ("_")
This code is for a hangman game Thanks in advance
Upvotes: 0
Views: 758
Reputation: 1322
You can prevent printing on a separate line by adding
, end=""
at the end of the print() function, as in:
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'])
which returns:
HE___
Upvotes: 3