Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hangman
#1
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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)
Reply
#2
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?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hangman metro17 4 4,281 Sep-18-2019, 10:59 AM
Last Post: perfringo
  Trouble coding hangman Tay 1 2,884 Mar-28-2019, 01:57 AM
Last Post: ichabod801
  Hangman code problem KrakowKid 1 2,938 Feb-25-2019, 06:29 PM
Last Post: ichabod801
  Python hangman help A1395 11 10,499 Feb-13-2019, 04:24 PM
Last Post: ichabod801
  Hangman Help. 2skywalkers 4 5,905 Jun-26-2018, 02:49 AM
Last Post: ichabod801
  Designing Hangman game spacetimeguy 2 6,188 Feb-02-2018, 08:55 PM
Last Post: spacetimeguy
  TKinter Hangman Game Bumble 1 23,421 Jul-19-2017, 06:56 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020