Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I wanted to print this tuple in a single line

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading