Not sure if you can to be honest... but probably the best thing is to handle the input afterwards:
You can define an exception to do this, or just trim/delete the content as you see fit...
Code:
#!/usr/bin/env python
class LengthError(Exception):
def __init__(self, value):
self.value = value
user_input = raw_input("Enter 5 characters of text: ")
if len(user_input) > 5:
raise LengthError("Input stream too long")
print user_input
gives:
Code:
gp@mariachi:~$ ./error.py
Enter 5 characters of text: 12345
12345
gp@mariachi:~$
gp@mariachi:~$
gp@mariachi:~$ ./error.py
Enter 5 characters of text: 123456
Traceback (most recent call last):
File "./error.py", line 11, in <module>
raise LengthError("Input stream too long")
__main__.LengthError
gp@mariachi:~$
gp@mariachi:~$
Bookmarks