# Putting unknown number of lines of input into a for loop

Page 1 of 1

## 4 Replies - 6206 Views - Last Post: 10 December 2012 - 08:12 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=303122&amp;s=6ecd77fa3cf2d7f4087ad8232c882257&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Brilliantwarrior

Reputation: 0
• Posts: 11
• Joined: 05-December 12

# Putting unknown number of lines of input into a for loop

Posted 07 December 2012 - 09:52 AM

I'm new to computer programming and Python. I'm using a tutorial site, which gives me exercises to complete, to learn Python 3. My current task is to do the following:For this program, the first line of input is an integer width. Then, there are some lines of text; the line "END" indicates the end of the text. For each line of text, you need to print out a centered version of it, by adding periods .. to the left and right, so that the total length of each line of text is width. (All input lines will have length at most width.) Centering means that the number of periods added to the left and added to the right should be equal if possible; if needed we allow one more period on the left than the right.I'd like to know how to loop an unknown number of lines of input into a for loop.
width=int(input())
a=str(input())
lenLin=len(a)
aopor=width-lenLin
aopor=aopor//2
aopor=aopor*"."
a=a+aopor
a=aopor+a
lenLin=len(a)
if lenLin<width:
a="."+a
print(a)

Is This A Good Question/Topic? 0

## Replies To: Putting unknown number of lines of input into a for loop

### #2 woooee

Reputation: 46
• Posts: 171
• Joined: 21-November 12

## Re: Putting unknown number of lines of input into a for loop

Posted 07 December 2012 - 10:32 AM

First, decide which loop you want to use Opening and reading files

### #3 chan 06

Reputation: 2
• Posts: 67
• Joined: 07-October 12

## Re: Putting unknown number of lines of input into a for loop

Posted 09 December 2012 - 09:05 AM

While loop it will loop until the condition returns false similar to Boolean Statement rather the variable is True or False.

Here is a simple two lines code that I made, that I have created an infinity loop (will repeat itself forever, until you break the program) where the condition will always return True. So you will be printing always Hello World!

while(True):
print("Hello World!")

Output
Hello World!
(and so on)

If you don't want it to last forever, than setup a condition, how many times would you like to repeat, here is another example:

counter = 0
while(counter < 5):
print("Hello World!")
counter += 1

Output:
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

I declared a variable called counter, which it's value is zero. Whatever is inside of the condition it will either return True or False. So here I said, if counter is less than 5, it will return the value True.

counter = 1 True
counter = 2 True
counter = 3 True
counter = 4 True
counter = 5 True
counter = 6 False

Five is not greater than six, therefore it's value will return False, therefore it will stop running. Therefore you will repeat this five times.

Now that you know a little bit about the While loop, let's move on to For Loop (Condition Loop). Basically is similar to While loop, there only difference is that there is a range of numbers. Finally the variable of loop is called i.

for i in range (0,6):
print(str(i) + ": Hello World")

Output:
0: Hello World
1: Hello World
2: Hello World
3: Hello World
4: Hello World
5: Hello World

This time, I created a for loop in range of 6 elements (included zero), therefore 0,1,2,3,4,5 and excludes the value 6. Therefore it will print "Hello World", six times. One thing neat about the For Loop, you can reverse the order, meaning you can start the last element, and decrease whatever value you want, unlike while loop.

for i in range (6, -1, -1):
print(i)

Output:
6
5
4
3
2
1
0

Here you can see visually see the For loop working, backwards. So you declared your initial value, followed by the value you want to stop therefore zero. And finally how much you like to increment.

Finally, I hope this helps you a lot the difference between While and For loops. They both can be useful in various scenarios.

### #4 Hqtitan

Reputation: 11
• Posts: 51
• Joined: 28-August 10

## Re: Putting unknown number of lines of input into a for loop

Posted 09 December 2012 - 03:26 PM

If you put the lines into a list, it can be as simple as this:

for i in range(len(inputList)):

### #5 jon.kiparsky

• Beginner

Reputation: 11039
• Posts: 18,851
• Joined: 19-March 11

## Re: Putting unknown number of lines of input into a for loop

Posted 10 December 2012 - 08:12 AM

Quote

I'd like to know how to loop an unknown number of lines of input into a for loop.

Something like this will work:

>>> for line in open(filename, 'r').read().split("\n"):
...     print line  # or do whatever with the line

You can do it in more explicit steps:

f =  open(filename, 'r')  # get a file handle for your file
lines = text.split("\n"): # split the text on line breaks to make a list of strings
for line in lines:        # loop over the lines and print them
print line

If you find you're doing "read a file and return it as a list of lines" more than once, you'd probably want to define a method to handle that. Generally, anything you do more than once is best handled in a function.

If you're taking lines from the command line, there's a few ways to do it.
One would be to read lines until one of them is "END", adding each line to a list of strings, and then process that list (take the number, use it to center the input lines, remember not to output "END" if it's in the list)

Or you could do it as a stream: read the integer, then read lines and center them until one of them is "END", and then stop.

This post has been edited by jon.kiparsky: 10 December 2012 - 08:20 AM