-3
def play(word):
    
    print(draw_hangman(tries))
    print(' '.join([i for i in wordcounts]))
    print(word)  
    print("\n")

    while not guesses and tries > 0:
        guess = input("Guess a letter or word\n> ").upper()
        if len(guess) == 1 and guess.isalpha():
            if guess in guessed_letters:
                print("You already guessed the letter", guess)

            else:
                print("Nice!!", guess, "is in the word!!")
                guessed_letters.append(guess)
                word_as_list = list(wordcounts)
                var = [i for i, letter in enumerate(word) if letter == guess]
                for index in var:
              

Im doing a hangman project but the characters that it print out doesn't stick above the underscore and I also want to space them out for each character.

MagTrick
  • 13
  • 3
  • Hi. You'll have to specify the problem you're having. Most people will likely not run and debug your code. What output or error are you getting? – pylang Nov 24 '20 at 01:40
  • 2
    Where do you want this again? What characters do you want spaces/characters after? Could you also condense a code into a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? – Ayush Garg Nov 24 '20 at 01:41
  • 1
    newCH = " " + character ; – 0x00001F Nov 24 '20 at 01:44
  • String concatenation is covered in any tutorial on Python strings. Stack Overflow is not intended to replace existing tutorials and documentation. – Prune Nov 24 '20 at 01:45
  • Are you trying to have a space after this underscore? `wordcounts = "_" * len(word)` If so, just give `wordcounts = "_ " * len(word)` and it will create `_ _ _ _ _ _` if len(word) is 6 – Joe Ferndz Nov 24 '20 at 01:48
  • If you want to print `'_ _ _ _'` where `wordcounts = '____'`, then you can give `print (' '.join([i for i in wordcounts]))`. This will print `'_ _ _ _'` – Joe Ferndz Nov 24 '20 at 01:52
  • Does this answer your question? [Which is the preferred way to concatenate a string in Python?](https://stackoverflow.com/questions/12169839/which-is-the-preferred-way-to-concatenate-a-string-in-python) – Joe Ferndz Nov 24 '20 at 01:54
  • @JoeFerndz yea that works for the underscore but for the character, the format of the word that supposes to be print above the underscore will be messed up and not matching up the underscore that supposes to stick below it. – MagTrick Nov 24 '20 at 03:06
  • @MagTrick, you need to replace the letter at the correct position. That way it will either print an underscore or the word. – Joe Ferndz Nov 24 '20 at 03:18
  • @JoeFerndz I can replace the letter but the letter is not in the correct position – MagTrick Nov 24 '20 at 03:25
  • I will use the answer section to respond to your question. Let me explain where you are making a few mistakes. – Joe Ferndz Nov 24 '20 at 03:53

2 Answers2

0

Is it that difficult to need a function ? Or am I missing the plot altogether ? Here is what I think, if I understood the requirement correctly. What I understood is to add a space after every alphabet and underscore in a sting.

text = 'abcdc123_34AB123_as'
re.sub(r'([A-Za-z_])',r'\1 ',text)

Output

'a b c d c 123_ 34A B 123_ a s '
Johnson Francis
  • 249
  • 3
  • 17
-1

I reviewed your code. Here are the changes you need to make to your code.

print("Welcome to the hangman game!! ")
print("In this game you can only miss 6 words and if you missed more than 6 you will lose!!")
print()  #added this line

#print(draw_hangman(tries))               #delete this line
#print(' '.join([i for i in wordcounts])) #delete this line
#print(word)                              #delete this line
#print("\n")                              #delete this line
print("The word has",len(word),"characters in it" )

while not guesses and tries > 0:
    print(draw_hangman(tries)) #this will always print the hangman
    print(' '.join([i for i in wordcounts])) #this will print the missing letters with underscore
    #print(word) # don't print the word. I assume you were printing for debugging
    print() #without the "\n" 
    guess = input("Guess a letter or word\n> ").upper()
    if len(guess) == 1 and guess.isalpha():

Also note that print() by default gives you a blank line.

Your code has print("\n"). This will give you two blank lines. You want to change it to just a print().

The output of this was:

Welcome to the hangman game!! 
In this game you can only miss 6 words and if you missed more than 6 you will lose!!

The word has 6 characters in it
_ _ _ _ _ _

Guess a letter or word
> m
Nice!! M is in the word!!
M _ _ _ _ _

Guess a letter or word
> o
Nice!! O is in the word!!
M O _ _ _ _

Guess a letter or word
> n
Nice!! N is in the word!!
M O N _ _ _

Guess a letter or word
> k
Nice!! K is in the word!!
M O N K _ _

Guess a letter or word
> e
Nice!! E is in the word!!
M O N K E _

Guess a letter or word
> y
Nice!! Y is in the word!!
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33