0

I writing a program for Hangman in Python 2.7. I'm trying to get the program to create the gallows and the hanged figure rather than me assembling them as a string and placing them in a list. My problem is the output from successive print statements gives me what I want but when I put the code in a function the result is skewed e.g.

print chr(47).rjust(4), # Right arm          
print chr(124), # backbone
print chr(92) + chr(124) # Left arm + tree 

gives me this /|\ | --- inner slashed are concatenated

In a function:

def erect_gallows(bad_guess):
    if bad_guess == 1:
        print chr() - - -
        .....
        .....  

this gives me this -- / | \ | with the slashes separated, out of alignment. Don't know why the print statements work outside but not inside the function. The function does not return anything, it just prints.

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
LennyC
  • 47
  • 4
  • Please supply the actual code, reduced to the minimum needed to show the problem. What you've posted so far doesn't do it. I get the same output from both the main and function versions. – Prune Jul 01 '16 at 23:15

1 Answers1

0

My implementation of your code and a straight solution. In short, I suggest that you quit fooling around with separate output fields in the print statement: just build the string you want and print it.

def erect_gallows(bad_guess):
  if bad_guess == 1:
    print chr(47).rjust(4), # Right arm          
    print chr(124), # backbone
    print chr(92) + chr(124) # Left arm + tree 


print "---   Main code   ---"
print chr(47).rjust(4), # Right arm          
print chr(124), # backbone
print chr(92) + chr(124) # Left arm + tree 
print "---   Func code   ---"
erect_gallows(1)
print "---   1-line code   ---"
print chr(47).rjust(4) + chr(124) + chr(92) + " " + chr(124)

Output:

---   Main code   ---
   / | \|
---   Func code   ---
   / | \|
---   1-line code   ---
   /|\ |
Prune
  • 76,765
  • 14
  • 60
  • 81