HackerRank-Python

Prepare > Python > Errors and Exceptions > Exceptions

stem_sw 2023. 8. 22. 20:35
 

Exceptions | HackerRank

Handle errors detected during execution.

www.hackerrank.com

 

문제


Task

You are given two values a and b.
Perform integer division and print a/b.

Input Format

The first line contains T, the number of test cases.
The next T lines each contain the space separated values of a and b.

Output Format

Print the value of .
In the case of ZeroDivisionError or ValueError, print the error code.

=> 두 숫자들을 를 T개 줄 건데 a/b 출력하고, ZeroDivisionError 또는 ValueError 에러나면 에러코드 출력해라.

 

 

 

 

코드


T = int(input())

for i in range(T):
    a, b = input().split()
    
    try:
        print(int(a)//int(b))
    except Exception as e:
        print('Error Code: ' + str(e)
        )

 

 

 

 

노트


try except

try:
	예외가 발생 할 수 있는 코드
except:
	예외 발생 시 실행 할 코드

에러가 발생할 것 같은 코드를 실행할 때, 중간에 멈추지 않게끔 하는 용도

 

 

 

 

 

참조


 

 

<python>try except 구문

코드를 치다가 궁금해서 정리해보는 try-except와 if-else의 차이!파이썬에서 프로그래밍 하면서 다양한 에러가 발생할 수 있는데 에러가 발생하는 상황에서 이 예외를 처리할 수 있는 구문이 try-exce

velog.io

https://github.com/sknsht/HackerRank/blob/master/Python/Errors%20and%20Exceptions/Exceptions/Solution.py

구글 바드