Guidelines

μ„œλ‘œ λ‹€λ₯Έ νƒ€μž…μ˜ μ—°μ‚°μœΌλ‘œ μΈν•œ TypeError

TypeErrorλŠ” 주둜 μˆ«μžμ™€ λ¬Έμžμ—΄, λ˜λŠ” λ‹€λ₯Έ ν˜Έν™˜λ˜μ§€ μ•ŠλŠ” 데이터 νƒ€μž…λ“€ μ‚¬μ΄μ˜ 연산을 μ‹œλ„ν•  λ•Œ λ°œμƒν•©λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄, μ•„λž˜μ™€ 같이 λ¬Έμžμ—΄(apple)κ³Ό 숫자(ten)λ₯Ό λ”ν•˜λ©΄ TypeErrorκ°€ λ°œμƒν•©λ‹ˆλ‹€.

TypeError μ˜ˆμ‹œ
ten = 10 apple = "사과" result = apple + ten # TypeError: unsupported operand type(s) for +: 'int' and 'str'

TypeError 처리 방법

TypeErrorλ₯Ό λ°©μ§€ν•˜κΈ° μœ„ν•΄μ„œλŠ” 연산을 μˆ˜ν–‰ν•˜κΈ° 전에 데이터 νƒ€μž…μ„ μΌμΉ˜μ‹œν‚€κ±°λ‚˜, μ μ ˆν•œ λ³€ν™˜μ„ μˆ˜ν–‰ν•΄μ•Ό ν•©λ‹ˆλ‹€.

데이터 νƒ€μž… λ³€ν™˜ μ˜ˆμ‹œ
number = 10 text = "사과" result = str(number) + text # '10사과'

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result