Hi, I am still very new to python and programming so could anyone help me on how to input lines from a user, one line at a time until they type "." on a separate line. I also wanted to count how many lines they entered.

What I tried was looping the input in a while loop and then increasing the line_count variable by 1 each time. However I could n't get it to work.

Thanks.

Edited 7 Years Ago by axa121: n/a

hi,
next time please post your code so that we may give you some advice on how to proceed, as for now here's one way of doing this:

linesCount = 0
print "Enter text ending in a '.':"
line = raw_input()

while line != ".":
    linesCount += 1
    line = raw_input()

print "The number of lines is", linesCount

hope this helps :)

Edited 7 Years Ago by masterofpuppets: n/a

Something along these lines:

lineCount = 0
while True:
    theTxt = raw_input("")
    if theTxt == ".":
        break
    lineCount += 1
    print theTxt*3

Obviously you can replace the print theTxt*3 with whatever you want, but that's the jist of it.

Many thanks, to both of you. Lastly what would be the best way to store the lines which were input, as I'm going to manipulate the text, e.g. remove uppercase letters and so on.

Should I add each line together? Or should I make the program change each line one at a time.

Thanks.

well you can edit the lines one by one when they are being input or you can store all of them in a list like this:

lines = []
linesCount = 0
print "Enter text ending in a '.':"
line = raw_input()
 
while line != ".":
    lines.append( line )
    linesCount += 1
    line = raw_input()
 
print "The number of lines is", linesCount
print lines

then you can have a for loop to go through all lines in the list :)

Thanks, better go to sleep now. Will carry one with this tomorrow.

from string import *

# Program to index sentences

stopWords = [ "a", "i", "it", "am", "at", "on", "in", "to", "too", "very", \
              "of", "from", "here", "even", "the", "but", "and", "is", "my", \
              "them", "then", "this", "that", "than", "though", "so", "are" ]

puntuation = [ ".", ",", ":", ";", "!", "?", "&", "'"]

stemWords = [ "s", "es", "ed", "er", "ly", "ing"]

print "Indexer: type in text, finish with a . at start of text only."

# Input text from a user

listCount = 0

text = raw_input()
mylist = []

while text != ".":
    mylist.append(text)
    listCount += 1
    text = raw_input()

print mylist

# Remove punctuation

removedtext = ""
for sentence in mylist:
    for char in sentence:
        if char not in punctuation:
            char = lower(char)
            removedtext = removedtext + char

# Remove Stop words

newlist = []
x = removedtext.split(" ")
for item in x:
    if item not in stopWords:
        newlist.append(item)

print newlist

Sorry for some of the bad variable names. This is what i have at the moment however when I go to remove the punctuation the last word of each line is joined together with the first word of the next line.

I'm also trying to remove any words which appear in the stopWord list, I think I have that working ok.

Thanks.

Hi,
the second part works fine :)
as for the first part I suggest using a list as well, instead of a string to hold the new sentence, it's much easier and you don't have to worry about the end of the sentence. Here's what I mean:

# Get the input in the same way
#..

# Remove punctuation
 
removedtext = []        # Use a list instead
for sentence in mylist:
    tmp = ""       # Here's the temporary string to hold a single sentence in
    for char in sentence:
        if char not in punctuation:
            char = lower(char)
            tmp = tmp + char         # Same check just add char to tmp instead
    # Here just add the sentence to the list
    removedtext.append( tmp )
     
# Remove Stop words
newlist = []
# Of course now this slightly changes, i.e. check every sentence in removedtext
for item in removedtext:
    tmpSentence = item.split( " " )     # Split it up in different words...
    for each in tmpSentence:         # Check whether the word is in the stopWords list
        if each not in stopWords:
            newlist.append( each )
 
print newlist

I think it's easier this way :)

An alternative would be to add an extra statement in the first loop like this:

removedtext = ""
for sentence in mylist:
    for char in sentence:
        if char not in punctuation:
            char = lower(char)
            removedtext = removedtext + char
    removedtext = removedtext + " "   #Separate the two sentences by a whitespace

but here the second loop changes slightly as well

# Remove Stop words
 
newlist = []
x = removedtext.split(" ")
for item in x:
    if item not in stopWords and item != "":    # you'll have a whitespace here it you add it to the string in the first loop
        newlist.append(item)
 
print newlist

hope this helps :)

Edited 7 Years Ago by masterofpuppets: n/a

Thanks once a again, I used the first method that you showed. I was trying to add an empty space before the loop had begun.

Would laugh so hard if masterofpuppets was Quintin Cutts

:) :) I'm not don't worry, but Quintin was one of my lecturers for Python last year :D

This article has been dead for over six months. Start a new discussion instead.