0

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

Janet Hamrani
  • 79
  • 1
  • 5
  • 11
    Possible duplicate of [Python print on same line](https://stackoverflow.com/questions/5598181/python-print-on-same-line) – Andrew Li Jun 28 '17 at 01:25

1 Answers1

3

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___
Larry
  • 1,312
  • 2
  • 15
  • 22