2017-12-07 2 views
-6

컴퓨터 서클에서이 문제가 발생하는 데 문제가 있습니다. 매우 간단한 운동이지만 잘 배우려는 것으로 알고 있습니다.Python에서 줄을 건너 뛰는 방법

하나의 입력에 아무 것도 입력되지 않으면 문제가 발생합니다. 마지막 입력은 이므로 오류를 방지하기 위해 다른 입력 (예 : goto)을 건너 뛰는 방법을 만들고 싶습니다.

최적화/구조 개선을위한 제안 사항을 알려 주시면 감사하겠습니다.

미리 감사드립니다.

번호 :이 프로그램

width = int(input()) 
second = input() 
third = input() 
fourth = input() 
fifth = input() 
sixth = input() 


lenght_of_second = len(second) 
lenght_of_third = len(third) 
lenght_of_fourth = len(fourth) 
lenght_of_fifth = len(fifth) 
lenght_of_sixth = len(sixth) 


periods = width - lenght_of_second 

periods_on_side = periods // 2 
reminder_periods = periods % 2 
if second != "END": 
    print("." * reminder_periods + "." * periods_on_side + second + periods_on_side * ".") 






periods = width - lenght_of_third 

periods_on_side = periods // 2 
reminder_periods = periods % 2 
if third != "END": 
    print("." * reminder_periods + "." * periods_on_side + third + periods_on_side * ".") 




periods = width - lenght_of_fourth 

periods_on_side = periods // 2 
reminder_periods = periods % 2 
if fourth != "END": 
    print("." * reminder_periods + "." * periods_on_side + fourth + periods_on_side * ".") 




periods = width - lenght_of_fifth 

periods_on_side = periods // 2 
reminder_periods = periods % 2 
if fifth != "END": 
    print("." * reminder_periods + "." * periods_on_side + fifth + periods_on_side * ".") 




periods = width - lenght_of_sixth 

periods_on_side = periods // 2 
reminder_periods = periods % 2 
if sixth != "END": 
    print("." * reminder_periods + "." * periods_on_side + sixth + periods_on_side * ".") 

는 입력의 제 라인 폭의 정수이다. 그런 다음 몇 줄의 텍스트가 있습니다. 줄은 텍스트의 끝을 나타냅니다.

텍스트의 각 줄에 대해 텍스트의 각 줄의 전체 길이가 너비가되도록 ..을 왼쪽과 오른쪽에 추가하여 가운데 맞춤 버전을 인쇄해야합니다. (모든 입력 라인 길이는 최대 너비입니다.)

센터링은 가능한 경우 왼쪽에 추가되고 오른쪽에 추가되는 마침표 수가 동일해야 함을 의미합니다. 필요한 경우 우리는 오른쪽보다 왼쪽에 하나 더 많은 기간을 허용합니다. 예를 들어, 입력

:

13 
Text 
in 
the 
middle! 
END 

올바른 출력은 내가 십년 나중에 올 수 있지만 난 여전히 당신에게 간단한 답을 제공 할 것입니다

.....Text.... 
......in..... 
.....the.....   
...middle!... 
+2

또한 코드를 추가 할 수 있습니까? –

+5

당신의 문제의 [mcve]를 보여주십시오 ... – Shadow

+0

""예를 들어, 입력 "".. 텍스트가 잘 리거나, 예제 입력을 완료하십시오 – davedwards

답변

0

것이다 당신의 질문 : 사용 루프.

width = int(input("Introduce the width:")) 
lines = [] 
lastline = input("Write a line of text: ") 
while lastline != "END": 
    lines.append(lastline) 
    lastline = input("Write a line of text: ") 
for i in lines: 
    print(("{:.^%d}" % width).format(i)) 

질문에서 예상대로 작동합니다. 나는 그것이 누군가를 돕기를 바랍니다 :)

관련 문제