简体   繁体   中英

how to change the position of the cursor in python 3

I am on windows 10 and I prefer not to install a new module (standard library solutions are accepted). I want the text that the user enters to start at the end of the third line.

My code:

print(" Enter password to unlock the Safe .\n\n password : \n\n\t2 attempts remaining .")
# code to move the cursor to the end of " password : " goes here
x = input()

output:

在此输入图像描述

wanted output:

在此输入图像描述

Also ANSI escape sequences don't seem to work without colorama(which unfortunately is an external module).

On Windows 10 you can use ANSI escape sequences as found in Console Virtual Terminal Sequences .

Before using them you need subprocess.run('', shell=True) (prior to Python 3.5, use subprocess.call ). These sequences make possible what you are looking for.

Caution: Also original Microsoft, the documentation of Erase in Display and Erase in Line is partly faulty. The parameters 1 and 2 are reversed.

This should work (although, actually entering the password seems to destroy the layout):

import subprocess
subprocess.run('', shell=True)
print(' enter password : \0337', end='')
print('\n\n\t2 attempts remaining .\0338', end='')
x = input()

用Python 3使用;

print("...", end="")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM