2

I've read many other articles on here about how to move the position of the input, but I've only seen how to move it back. I tried using "\b" for this but it doesn't work, can anyone help? btw I'm using repl.it,I'm not sure what version of python it uses

choice=int(input("╒══════════╤══════════╤══════════╕\n"
                +"│1 - ATTACK│ 2 - HEAL │ 3 - FLEE │\n"
                +"╞══════════╧══════════╧══════╤═══╡\n"
                +"│ Enter your choice here --> │ X │\n"
                +"╘════════════════════════════╧═══╛"))
Pricey
  • 21
  • 2
  • With "moving input" are you talking about changing the input text or making the line where the user types to a new line? – The Peeps191 Jul 22 '22 at 19:05
  • 2
    @ThePeeps191 they want to move the cursor to where the X is in the box, so the input text is outputted there – Ryan Jul 22 '22 at 19:07
  • 2
    AFAIK, there's no way to move the cursor using the standard `input()` and other stream-oriented console output and input. You can do this using the [curses](https://docs.python.org/3.8/library/curses.html) standard library, however. – Schol-R-LEA Jul 22 '22 at 19:08

1 Answers1

1

You can use ANSI escape codes to move the cursor around. The Wikipedia Page has a lot of good information about how they work, along with a table that contains all the relevant escape codes to help you.

import os
import msvcrt

# Make sure that ANSI codes work
os.system('')

# We will move 1 line up and to the 32nd column
go_to_X = "\033[F" + "\033[32G"

# Print the message and the escape sequence
# Use end="" so that Python does not add an extra newline at the end of the string
print("╒══════════╤══════════╤══════════╕\n"
    +"│1 - ATTACK│ 2 - HEAL │ 3 - FLEE │\n"
    +"╞══════════╧══════════╧══════╤═══╡\n"
    +"│ Enter your choice here --> │ X │\n"
    +"╘════════════════════════════╧═══╛" + go_to_X, end="")

# Read one character from STD input (getch = "Get char")
# If you are on linux, then use getch.getch()
choice=int(msvcrt.getche())
Xiddoc
  • 3,369
  • 3
  • 11
  • 37