파이썬 알고리즘 085 | [백준 10951] A+B - 4 (EOF)

Yunny.Log ·2021년 1월 27일
0

Algorithm

목록 보기
88/318
post-thumbnail

문제 A+B - 4

두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.

입력
입력은 여러 개의 테스트 케이스로 이루어져 있다.

각 테스트 케이스는 한 줄로 이루어져 있으며, 각 줄에 A와 B가 주어진다. (0 < A, B < 10)

출력
각 테스트 케이스마다 A+B를 출력한다.

예제 입력 1
1 1
2 3
3 4
9 8
5 2
예제 출력 1
2
5
7
17
7
출처
문제를 만든 사람: baekjoon

<내 풀이>


EOF 에러에서 벗어날 수 없다
=>try-except을 쓰면 됨


a,b=map(int,input().split())
while a and b:
    try:
        print(a+b)
        a,b=map(int,input().split())
    except EOFError: #굳이 이거 안쓰고 그냥 error 써도 됨
        break

<풀이>

=> 출처 : 링크텍스트

(1)


while True:
    try:
        a, b = map(int, input().split())
        print(a+b)
    except:
        break

(2)


import sys

for i in sys.stdin:
    a, b = map(int, i.split())
    print(a+b)

<반성점>

  • while loop에서 많이 약한 것 같다
  • while a and b로 돌렸는데, 자꾸 EOF 에러가 발생한다, 원인은 알지 못하겠음

<배운 점>

링크텍스트

  • A tip: when you are dealing with EOF or something alike always remember that, to python, every null or empty or zero it's managed like a "false"... So you can put something like "while s:" because, if it's null or empty, will be false to python

  • One pattern would be:

s = input()
while s:
  somelist.append(s) 
  s = input()

It's a bit annoying to write the same line twice, but there is no 'do while' like in c.

while True:
  s = input() 
  if not s:
    break
  somelist.append(s)

whileT True 구문에서 결국 무한반복되어서 에러 발생함 => 따라서 에러가 발생해도 종료되지 않도록 try-except문을 사용해주면 됨

0개의 댓글