1

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?

user3586056
  • 21
  • 1
  • 4

2 Answers2

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