4

For practice, I'm trying to do some stuff in Python. I've decided to make a simple hangman game - I'm not making a GUI. The game would start with a simple input(). Now, I'd like next line to, beside asking for input, to delete the hidden word. I've tried using \b (backspace character), but it's not working. Something like:

word = input("Your word: ")
for i in range(len(word) + 12):
    print("\b")

Now, printing the backlash character is supposed to delete the input and "Your word", but it isn't doing anything. If I do this in IDLE I get squares, and I get nothing if I open it by clicking.

How to accomplish this? I'm afraid I wasn't too clear with my question, but I hope you'll see what I meant. :)

Bane Bojanić
  • 712
  • 1
  • 8
  • 20
  • 1
    Comment unrelated to your question: you probably want to use `raw_input()` instead of `input()`, unless you're using Python 3. – Fred Larson Jun 28 '10 at 21:21
  • 1
    It is Python 3, notice that print is a function. But that's really irrelevant here. I want to know both Python 3.x and lower versions, but as I'm just learning, I'm learning/using Python 3 as my primary version, because that's the future. :) – Bane Bojanić Jun 28 '10 at 21:30
  • After I commented, I noticed the parentheses on `print`. But that's also valid Python 2, so it's ambiguous which version you were asking about. I also think you have your parentheses misplaced on the `len` expression. – Fred Larson Jun 28 '10 at 21:39
  • I know it's valid, but rarely used, especially for printing only a single string. And yes, you're right about the len(), I fixed it. – Bane Bojanić Jun 28 '10 at 21:49

4 Answers4

4

\b does not erase the character before the cursor, it simply moves the cursor left one column. If you want text entry without echoing the characters then look at getpass.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • best idea. @Edol: to elaborate a bit more, use `from getpass import getpass; word = getpass('Enter hidden word:')` – Nas Banov Jun 29 '10 at 03:15
2

I assume the player entering the word wants to be sure they've entered it correctly so you probably want to display the word as they're typing it right?

How about printing enough \ns to move it off the screen when they're done or issue a clear screen command?

You mentioned this was a simple game so a simple solution seems fitting.

[Edit] Here's a simple routine to clear the console on just about any platform (taken from here):

def clearscreen(numlines=100):
    """Clear the console.
    numlines is an optional argument used only as a fall-back.
    """
    import os
    if os.name == "posix":
        # Unix/Linux/MacOS/BSD/etc
        os.system('clear')
    elif os.name in ("nt", "dos", "ce"):
        # DOS/Windows
        os.system('CLS')
    else:
        # Fallback for other operating systems.
        print '\n' * numlines
Jon Cage
  • 36,366
  • 38
  • 137
  • 215
2
word = raw_input("Your word: ")
import sys
sys.stdout.write("\x1b[1A" + 25*" " + "\n")

This will replace the last line printed with 25 spaces.

Forrest Voight
  • 2,249
  • 16
  • 21
  • 1
    This works for me with `print` as well as with `sys.stdout.write`. It might be dependent on the terminal, though. – Fred Larson Jun 28 '10 at 22:07
  • Eww. This does not work on windows. In fact it will work only on terminal that supports ANSI escape sequences – Nas Banov Jun 29 '10 at 03:12
1

I think part of your problem is that input is echoing the Enter that terminates your word entry. Your backspaces are on another line, and I don't think they'll back up to the previous line. I seem to recall a SO question about how to prevent that, but I can't find it just now.

Also, I believe print, by default, will output a newline on each call, so each backspace would be on its own line. You can change this by using an end='' argument.

Edit: I found the question I was thinking of, but it doesn't look like there's any help there. You can look at it if you like: Python input that ends without showing a newline

Community
  • 1
  • 1
Fred Larson
  • 60,987
  • 18
  • 112
  • 174