python script to restrict keyboard input to a specified length of characters
hi..
i am trying to write a script, which accepts user input from keyboard. similar to this.
http://stackoverflow.com/questions/8...rs-and-lengths
however, my idea is to stop the cursor in the output terminal, after it reaches a specified length (say 16 characters).
how do i do this? please help!
Re: python script to restrict keyboard input to a specified length of characters
Moved to programming talk
Re: python script to restrict keyboard input to a specified length of characters
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:~$