Python Forum
Hangman - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Hangman (/thread-13534.html)



Hangman - 2skywalkers - Oct-19-2018

Im not sure if this should be posted in the games section but the only library its using is random. I got this code online to try to understand it and then write my own but I came across a problem.

This is what happens when i guess all the right letters

____t__tte_ttestYou won


What i want to happen is that after I guess one letter correct, I want it to print a new line below instead of right next to it, Ive tried stuff with newlines and stuff like that but have not figured it out. Original it was printer in a column so I added "end = ''" and that fixed that problem but now I have this problem.

name = input("What is your name? ")

print("Hello, " + name, "Time to play hangman!")

print ("")

import random

word = "test"


guesses = ''


turns = 10



while turns > 0:         

    failed = 0             

   
    for char in word:      

    
        if char in guesses:    
    
        
            print(char, end = '') 
            

        else:
    
        
            print("_", end = '')     

        
            failed += 1    

    

    
    if failed == 0:        
        print("You won")

    
        break              

    print

    guess = input("guess a character: ")

    
    guesses += guess                    

    
    if guess not in word:  
 
     
        turns -= 1        
 
  
        print("Wrong")  
 
    
        print("You have", + turns, 'more guesses') 
              
    
        if turns == 0:           
    
           
            print("You Lost, sad face")
            print("the word was:")
            print(word)



RE: Hangman - buran - Oct-19-2018

you added end='' argument to print function because it was printing in a column and now you are surprised it prints on the same line?


RE: Hangman - 2skywalkers - Oct-19-2018

No, I wanted it to print on the same line. Originally it was:

Guess a letter:
-
-
-
-

So I changed it. To Guess a letter: - - - - But now, when I get a correct guess, it just appends it to the current line, I want it to print a newline then instead of appending.

I want it to be like this:

(The word is test)

Guess a letter: e

-e--

Guess a letter: t

te-t

Like that.


RE: Hangman - ichabod801 - Oct-19-2018

Right, but that is what is putting "You won" on the same line as everything else. Put a plain print() at the end of the for loop to move the output to the next line.