I'm really new to Python so I don't know much of the syntax. I'm trying to write a program that accepts words in separate lines and adds each word to an array. I tried using isalpha() to break the loop after all the words were inputted. But the loop would break even if it was a word. Please help! Edit: I'd also like to know how to get rid of the \n in the output.
import sys; args = sys.argv[1:]
import fileinput
words = []
for line in fileinput.input():
if line.isalpha() == False:
fileinput.close()
words.append(line)
print (words)
Input: hello
Output: ['hello\n']
From the documentation for isalpha:
Return True if all characters in the string are alphabetic and there is at least one character, False otherwise.
A little work in IPython:
In [1]: 'hello\n'.isalpha()
Out[1]: False
In [2]: 'hello\n'.rstrip().isalpha()
Out[2]: True
I would fix the immediate problem with something like this:
for line in fileinput.input():
line = line.rstrip()
if line.isalpha() == False:
fileinput.close()
words.append(line)