Jared Parker
Jared Parker

Reputation: 442

python 3 How to print on a previous input line

I know you can print on the previous line by using end="" e.g

import time

print("Loading",end="")
for i in range(3):
    print(".",end="")

but is there a way to do it with an input, as it comes up with an error. e.g the user is asked a question then they input an answer and it prints a ✘ or ✔ next to the input.

#Doesn't work
print("What is the capital of england?")
ans = input("+-> ",end="")

if ans == "London":
    print("✔")
else:
    print("✘")

This by not be using end="" but a different way?

PS \r and \b don't work

Upvotes: 1

Views: 1159

Answers (3)

Kasravnd
Kasravnd

Reputation: 107287

Here is one way using ANSI/VT100 Terminal Control Escape Sequences

In [103]: def ask():
              CURSOR_UP_ONE = '\x1b[1A'
              ERASE_LINE = '\x1b[2K'
              message = "What is the capital of england? "
              answer = input(message)
              if answer == "London":
                  print(CURSOR_UP_ONE + ERASE_LINE + message + answer + "✔")
              else:
                  print(CURSOR_UP_ONE + ERASE_LINE + message + answer + "✘")
   .....: 

In [104]: ask()
What is the capital of england? Paris✘

Upvotes: 2

nikhil
nikhil

Reputation: 895

Your question is bit not clear.But as far my understanding below is the answer.

print("what is capital of England")
inp = input()

if(inp =="London"):
    print(inp+"->✔")
else:
    print(inp+"->✘")

Upvotes: 0

Until you use a new line after your input, Python will not read STDIN to take the input.

So, You can't print the message right after the input.

Upvotes: 0

Related Questions