-1

Im trying to create a hangman game in python and I cant figure out how each time I input a letter I can show the letters I have put horizontally and with the '_' under every missing word

import random

words = ['programming', 'tiger', 'lamp', 'television',
'laptop', 'water', 'microscope', 'doctor', 'youtube',
'projects']

random_word = random.choice(words)

print('our random word', random_word)

print('*********** WORD GUESSING GAME ***********')

user_guesses = ''
chances = 10

while chances > 0:
    wrong_guesses = 0
    for character in random_word:
        if character in user_guesses:
            print(f"Correct guess: {character}")
        else:
            wrong_guesses += 1
            print('_')

    if wrong_guesses == 0:
        print("Correct.")
        print(f"Word : {random_word}")
        break
    guess = input('Make a guess: ')
    user_guesses += guess

    if guess not in random_word:
        chances -= 1
        print(f"Wrong. You have {chances} more chances")

        if chances == 0:
            print('game over')
VangKont
  • 13
  • 2

2 Answers2

0

I assume by "horizontally", you mean put them on the same line.

So, basically, you don't want the print function to output "end of line" for you (which it does automatically).

You can do it like this:

for character in random_word:
    if character in user_guesses:
        print(f"{character} ", end='')
    else:
        wrong_guesses += 1
        print('_', end = '')

print('')

Notice the end parameter - by setting it to be an empty string, only the text you send will be printed and nothing else, so until you specifically add end of line, all text will appear on the same line.

Lev M.
  • 6,088
  • 1
  • 10
  • 23
  • I tried this and now it leaves tons of ( _) before the make a guess printf and it stacks all the answers in one line including the make a guess question @LevM. – VangKont Jan 09 '22 at 16:00
  • @VangelisKontonikolaou what do you mean "tons of"? It will print exactly as many _ characters as there are letters in the word you want the player to guess. Perhaps you need to better explain what you want. Add to your question an example of how you want the output to look. – Lev M. Jan 09 '22 at 18:31
0

You could make this prettier by printing the hangman state (gallows):

Setup a list of gallows states:

hung =   [ "+-------+      ",  #0
           "|       |      ",  #1
           "|       ^      ",  #2
           "|     (' ')    ",  #3
           "|       ~      ",  #4
           "|    __/ \\__   ", #5
           "|   /  | |  \\ ",  #6
           "|  /   ===   \\",  #7
           "| o   [___]   o",  #8
           "|     |   |    ",  #9
           "|     |   |    ",  #10
           "|     /   \\   ",  #11
           "|\\             ", #12
           "| \\____________"] #13

states   = [hung.copy()]                        # last state is hung
hung[9]  = hung[9].replace("|   |", "|    ",1)   
hung[10] = hung[10].replace("|   |","|    ",1)
hung[11] = hung[11].replace("\\"," ")
states.insert(0,hung.copy())                    # hide right leg
hung[9:12]  = ["|"]*3
states.insert(0,hung.copy())                    # hide left leg
hung[5]  = hung[5].replace("__ ","   ",)
hung[6]  = hung[6].replace("\\"," ",1)
hung[7]  = hung[7].replace("\\"," ",1)
hung[8]  = hung[8].replace("  o","   ",1)
states.insert(0,hung.copy())                   # hide right arm
hung[5]  = hung[5].replace("__","  ",)
hung[6]  = hung[6].replace("/"," ")
hung[7]  = hung[7].replace("/"," ")
hung[8]  = hung[8].replace("o"," ")
states.insert(0,hung.copy())                   # hide left arm
hung[5:9] = ["|"]*4
states.insert(0,hung.copy())                   # hide body
hung[2:5] = ["|"]*3
states.insert(0,hung.copy())                   # hide head (1st state)

Game play:

word   = "elephant".upper()      # select a random uppercase word here
hidden = dict.fromkeys(word,"_") # map "_" to letters still hidden
for s in states[:-1]:
    print(*s,sep='\n')           # show gallow
    while hidden:                # uncover letters
        mask = " ".join(hidden.get(c,c) for c in word) 
        letter = input(mask+" : ").upper()             # get a letter
        if letter not in hidden: break                 # bad letter -> for loop
        del hidden[letter]                             # uncover good letter 
    else:
        print(" ".join(word),"You win!!!") # no more hidden letters
        break
    print()
    print(f"Letter {letter} not found")    # bad letter -> next state
else:
    print(*states[-1],sep='\n') # last state -> loss
    print("You Lose !!!")

Sample run:

+-------+      
|       |      
|
|
|
|
|
|
|
|
|
|
|\             
| \____________
_ _ _ _ _ _ _ _ : e
E _ E _ _ _ _ _ : a
E _ E _ _ A _ _ : i

Letter I not found
+-------+      
|       |      
|       ^      
|     (' ')    
|       ~      
|
|
|
|
|
|
|
|\             
| \____________
E _ E _ _ A _ _ : t
E _ E _ _ A _ T : n
E _ E _ _ A N T : l
E L E _ _ A N T : g

Letter G not found
+-------+      
|       |      
|       ^      
|     (' ')    
|       ~      
|      / \     
|      | |    
|      ===    
|     [___]    
|
|
|
|\             
| \____________
E L E _ _ A N T : p
E L E P _ A N T : h
E L E P H A N T You win!!!
Alain T.
  • 40,517
  • 4
  • 31
  • 51