I would like to stop the user to type in too many characters in the input function so it just stops the input() when too many characters are put in but the characters previously typed in stay. In this case I wouln't want to check if this is after the user has pressed enter but I would like to interrupt the function. Is this somehow possible?
Asked
Active
Viewed 3,722 times
1
-
In other words, do you want to stop the input function as soon as the user has entered a certain number of characters? – Anderson Green Apr 29 '14 at 16:10
-
1I've found several questions on Stack Overflow that are similar to this one: https://www.google.com/#q=python+input+without+pressing+enter – Anderson Green Apr 29 '14 at 16:14
-
2@AndersonGreen http://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user serves as a somewhat better dupe... – Jon Clements Apr 29 '14 at 16:16
-
@AndersonGreen Yes that is what I want to achieve – user3586056 Apr 29 '14 at 18:43
2 Answers
0
I don't believe this is possible, but if you're concerned about a maximum length (for a database value or something similar) you can use slicing to address excessively long entries:
maxlen = 256
userval = input("Enter value (max 256):")[:maxlen]

g.d.d.c
- 46,865
- 9
- 101
- 111
-
I believe you can do it on the fly using ``readline``, but then you have all the problems that come with using ``readline``, namely that your code is now GPL v3 – aruisdante Apr 29 '14 at 16:23
-
Linking to `readline` does not place your code under GPL v3; it just requires that if distributed, it be distributed under a GPL-compatible license. – chepner Apr 29 '14 at 16:40
-
Sure, but you're still making it the most aggressive kind of open-source (copy-left, no attribution, must distribute source with executable). I'm constantly amazed that Python's doc for readline doesn't warn people about this. I wonder how many technically license-breaking applications there are out there? – aruisdante Apr 30 '14 at 21:12
0
Might be possible with a loop and msvcrt. Here is an example:
while len(string) < 10:
#Whatever length you want instead of 10
string += msvcrt.getch()

user2835118
- 944
- 1
- 6
- 7
-
When it will reach the end of the loop (max length) it will simply exit the loop, stopping the input. – user2835118 Apr 29 '14 at 16:26
-
-
But it works. it is the only way I see it being possible to the request, and its better than nothing :) – user2835118 Apr 29 '14 at 16:35