0

I am working on a python program. I want to take a user input which is less then 140 characters. If the sentence exceeds the word limit, It should just print the 140 characters. I am able to enter characters but this is what happens. I am new to python. How can I achieve this?

def isAlpha(c):
    if( c >= 'A' and c <='Z' or c >= 'a' and c <='z' or c >= '0' and c <='9'):
        return True
    else:
        return False


def main():
    userInput = str(input("Enter The Sentense: "))
    for i in range(140):
        newList = userInput[i]
        print(newList)

this is the output i get

Enter The Sentense: this is
t
h
i
s

i
s
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    main()
  File "C:/Users/Manmohit/Desktop/anonymiser.py", line 11, in main
    newList = userInput[i]
IndexError: string index out of range

Thank you for the help

Manmohit
  • 11
  • 5

3 Answers3

3
userInput = str(input("Enter The Sentense: "))
truncatedInput = userInput[:140]
AMADANON Inc.
  • 5,753
  • 21
  • 31
  • 2
    It may help to mention that this is called Python's *slice* notation, and give some [reference](http://stackoverflow.com/questions/509211/the-python-slice-notation). – Jonathon Reinhart Jul 08 '13 at 04:49
  • 1
    Also, `Sentense` should be `sentence` - no capital letter (it is not a "proper noun", and is not at the start of a sentence), and has a `c` after the `n`, not an `s`. – AMADANON Inc. Jul 08 '13 at 04:53
3

Why not just test for the len?

if len(input) > 140:
   print "Input exceeds 140 characters."
   input = input[:140]

You can also put up other errors using this or quit the program, if you want to. The input = input[:140] makes sure that only the first 140 characters of the string are captured. This is wrapped around in an if so that if the input length is less than 140, the input = input[:140] line does not execute and the error is not shown.

This is called Python's Slice Notation, a useful link for quick learning would be this.

Explanation for your error -

for i in range(140):
    newList = userInput[i]
    print(newList)

If the userInput is of length 5, then accessing the 6th element gives an error, since no such element exists. Similarly, you try to access elements until 140 and hence get this error. If all you're trying to do is split the string into it's characters, then, an easy way would be -

>>> testString = "Python"
>>> list(testString)
['P', 'y', 't', 'h', 'o', 'n']
Community
  • 1
  • 1
Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
2

for i in range(140) assumes there are 140 characters in the string. When you have finished iterating through the string, there won't be an index n, so an error is raised.

You can always iterate over a string:

for i in str(input("Enter a sentence: "))[:140]:
    print i

[:140] is Python's Slice Notation, which cuts the string from the first character to the 140th. Even if there is no 140th character, it just goes to the end of the string then.

Community
  • 1
  • 1
TerryA
  • 58,805
  • 11
  • 114
  • 143
  • +1 For explaining what the problem is, and suggesting a more elegant approach. –  Jul 08 '13 at 05:31