Janet Hamrani
Janet Hamrani

Reputation: 79

How to make no line break in python?

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

Answers (1)

Larry
Larry

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

Related Questions