开发者

How to query an input in Python without outputting a new line (cont.)

I already 开发者_StackOverflow中文版posted this, but here is the exact code:

x1 = input("")
x2 = input("-")
x3 = input("-")
x4 = input("-")

So, how would I do it so that there are no spaces between the first input and the next "-"?

Example:

1234-5678-9101-1121


Ugly, but you could use terminal escape sequences to delete the newline created by the user ending input between each successive call of input().

The appropriate sequences of escapes would be <Esc>[2K to erase the current line, and then possibly <Esc>[nC to move forwards n characters where n is calculated by retrieving the length of the string that the last input() call returned.


>>> x1, x2, x3, x4 = raw_input("Input number as xxxx-xxxx-xxxx-xxxx").split('-')

If you're using Python version 3.x, replace rawinput with input.


You can use the code in Python read a single character from the user to read stdin character by character (basically reading it in unbuffered mode so that the user doesn't have to type enter before you can see the input).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜