I wanted to print this tuple in a single line

Question:

I was making a small program to exemplify the wordle game in python but the code below generates an output in tuple format (one element per line). Like this:

T
_
E
T
E

the code:

def check_palpite():
    palavra_secreta = "Sorte"
    tentativas = 6
    while tentativas > 0:
        palpite = str(input("Faça um palpite de palavra de 5 letras!"))
        if palpite == palavra_secreta:
            print("Resposta correta")
            break
        else:
            tentativas = tentativas - 1
            print(f"Você tem {tentativas} tentativa(s) n ")
            for char, palavra in zip(palavra_secreta, palpite):
                if palavra in palavra_secreta and palavra in char:
                    print(palavra.upper())

                elif palavra in palavra_secreta:
                    print(palavra.lower())
                else:
                    print("_")
                if tentativas == 0:
                    print(f"Fim de jogo a palavra era {palavra_secreta}")

check_palpite()

I need the elements to be generated in a single-line format and not a tuple. Like this:

T_ETE

I believe that to solve this problem the responsible part of the code is this

for char, palavra in zip(palavra_secreta, palpite):
    if palavra in palavra_secreta and palavra in char:
Asked By: Mateus Lima

||

Answers:

You have two approaches, either:

  1. pass a string to the end= parameter of print(), or
  2. append each character to a string and print the string after the loop.

Approach 2 is faster because it makes fewer function calls, so I’ll use that one. Just create an empty string before the loop, then print that string at the end of the loop. Like this:

def check_palpite():
    palavra_secreta = "Sorte"
    tentativas = 6
    while tentativas > 0:
        palpite = str(input("Faça um palpite de palavra de 5 letras! "))
        if palpite == palavra_secreta:
            print("Resposta correta")
            break
 
        tentativas = tentativas - 1
        print(f"Você tem {tentativas} tentativa(s) n ")

        string = ""
        for char, palavra in zip(palavra_secreta, palpite):
            if palavra in palavra_secreta and palavra in char:
                string += palavra.upper()
            elif palavra in palavra_secreta:
                string += palavra.lower()
            else:
                string += "_"
 
            if tentativas == 0:
                print(f"Fim de jogo a palavra era {palavra_secreta}")
        print(string)


check_palpite()
Answered By: Michael M.