0

Here is my code:

print("Make a 20-digit code with numbers 0-9: |___|", end="\r", flush=True)
addDigits = input("Make a 20-digit code with numbers 0-9: |")

More specifically, I am trying to place the cursor like this, so the user types in-between the two |s:

"Make a 20-digit code with numbers 0-9: |(This is where the cursor should be)|"

(I do have import sys at the top, so it isn't that. Any ideas? Thank you.)

3
  • This question is similar to: Printing String after the user input on the same line. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 28 at 15:11
  • 2
    This will work in some terminals. Commented Oct 28 at 15:13
  • it may need to use modules like curses but it will need longer code (to check if it is digit and if there are only 4 items. And it may work only in some terminals. Commented Oct 29 at 22:19

1 Answer 1

2

Python supports the standard backspace character although your terminal may not.

Assuming your terminal does handle it then:

prompt = "Make a 20-digit code with numbers 0-9: |___|"
first_pipe = prompt.find("|") + 1
assert first_pipe > 0
bs = "\b" * (len(prompt) - first_pipe)
value = input(prompt + bs)
print(value)

In this example, there are 3 underscores in the prompt. There's nothing here that would prevent overrunning the prompt. You'd need to use some raw input tooling to handle that.

Sign up to request clarification or add additional context in comments.

2 Comments

It didn't work but thanks anyway.
@define_duck You will have noted the caveat in my answer that not all terminals support this approach. What is your OS and terminal type?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.