python3中input输入浅谈,如何在Python 3中输入文本时不使用input()

I'm making a basic utility in Python 3 where the user inputs a command and gets feedback printed out into the console. When entering data using the input() or sys.stdin.readline() functions this is what the command-line session may look like (including \r and \n characters)

1. What is your name:\n

2. \n

3. Your name is .\n

But, I would like to display a \r character after the user hits enter instead of the \n character, as shown on line 2. After the user had typed everything in and hit enter it would look like this

1. What is your name:\n

2. Your name is .\n

(because line 2 would have a \r character after the entered data, returning the cursur back to the far left)

Does anybody know of a way I might accomplish this?

解决方案

Well, I discovered this method although I am almost cirtain that the msvcrt module is for Windows only.

import msvcrt

import sys

def msgInput(prompt):

print(prompt, end='')

data= b''

while True:

char= msvcrt.getch()

if char != b'\r':

print(char.decode(), end='')

sys.stdout.flush()

data= data+char

else:

print('\r', end='')

break

return data.decode()

If anybody knows of any cross-platform methods, please share.

Update - Unfortunately this method has many limitations, such as the user cannot navigate the entered text with the arrow keys.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值