1 Replies - 951 Views - Last Post: 05 December 2012 - 04:16 PM Rate Topic: -----

#1 Brilliantwarrior  Icon User is offline

  • New D.I.C Head

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

Printing a Centered Version of Input Using Periods.

Posted 05 December 2012 - 03:41 PM

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.


The code outputs desired results for the first two lines of input, but doesn't print the rest of input. I'm not sure what to do from here.

width=int(input())
line1=str(input())
line2=str(input())
line3=str(input())
line4=str(input()) 
line5=str(input())
for a in [line1,line2,line3, line4, line5]:
   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: Printing a Centered Version of Input Using Periods.

#2 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: Printing a Centered Version of Input Using Periods.

Posted 05 December 2012 - 04:16 PM

Your print(a) statement is part of the if block (because of indentitation). So there will only be something printed when the if condition is True. But you alway want to print something. Just change the indentitation level of that print:
width=int(input())
line1=str(input())
line2=str(input())
line3=str(input())
line4=str(input()) 
line5=str(input())
for a in [line1,line2,line3, line4, line5]:
    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) #now correctly indented



There is another problem though: Your programm reads exactly 5 lines but the problem description requires you to do something else.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1