Guidelines

ν•¨μˆ˜λ‘œ μž¬μ‚¬μš© κ°€λŠ₯ν•œ μ½”λ“œ 블둝 λ§Œλ“€κΈ°

ν•¨μˆ˜λŠ” νŠΉμ • μž‘μ—…μ„ μˆ˜ν–‰ν•˜λŠ” μ½”λ“œ λΈ”λ‘μœΌλ‘œ, ν•„μš”ν•œ μˆœκ°„λ§ˆλ‹€ μ •μ˜λœ ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜μ—¬ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

ν•¨μˆ˜λŠ” μ½”λ“œ μž¬μ‚¬μš©μ„±μ„ 높이기 μœ„ν•œ μ€‘μš”ν•œ ꡬ성 μš”μ†Œμž…λ‹ˆλ‹€. ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜λ©΄ λ™μΌν•œ μ½”λ“œλ₯Ό λ°˜λ³΅ν•΄μ„œ μž‘μ„±ν•  ν•„μš” 없이, ν•„μš”ν•  λ•Œλ§ˆλ‹€ ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•΄ νŠΉμ • μž‘μ—…μ„ μ²˜λ¦¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

ν•¨μˆ˜ ꡬ성 μš”μ†Œ
def ν•¨μˆ˜μ΄λ¦„(λ§€κ°œλ³€μˆ˜): μ½”λ“œ 블둝

ν•¨μˆ˜λŠ” def ν‚€μ›Œλ“œλ‘œ μ‹œμž‘ν•˜λ©°, def ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•œ 쀄 λμ—λŠ” 콜둠(:)을 λΆ™μž…λ‹ˆλ‹€. ν•¨μˆ˜μ˜ μ½”λ“œ 블둝은 λ“€μ—¬μ“°κΈ°λ₯Ό μ‚¬μš©ν•΄ μž‘μ„±ν•©λ‹ˆλ‹€.

μ½”λ“œ 블둝 λ‚΄λΆ€μ—λŠ” ν•¨μˆ˜κ°€ μˆ˜ν–‰ν•  μž‘μ—…μ„ κ΅¬ν˜„ν•˜λ©°, ν•„μš”ν•œ 경우 λ§€κ°œλ³€μˆ˜λ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

ν•¨μˆ˜κ°€ μˆ˜ν–‰ν•œ μž‘μ—…μ˜ κ²°κ³Όλ₯Ό λ°˜ν™˜ν•˜κΈ° μœ„ν•΄μ„œλŠ” return ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.

ν•¨μˆ˜ μ •μ˜ μ˜ˆμ‹œ
def greet(name): # name λ§€κ°œλ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜λŠ” greet ν•¨μˆ˜ μ •μ˜ return f"μ•ˆλ…•ν•˜μ„Έμš”, {name}λ‹˜!" # name λ§€κ°œλ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ λ¬Έμžμ—΄ 생성 ν›„ λ°˜ν™˜ print(greet("μ½”λ“œν”„λ Œμ¦ˆ")) # μ•ˆλ…•ν•˜μ„Έμš”, μ½”λ“œν”„λ Œμ¦ˆλ‹˜!

μœ„ μ˜ˆμ‹œμ—μ„œ greet ν•¨μˆ˜λŠ” name λ§€κ°œλ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ λ¬Έμžμ—΄μ„ μƒμ„±ν•˜κ³ , μƒμ„±λœ λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•©λ‹ˆλ‹€.

μ •μ˜ν•œ ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•  λ•ŒλŠ” greet("μ½”λ“œν”„λ Œμ¦ˆ")와 같이 ν•¨μˆ˜ 이름 뒀에 κ΄„ν˜Έ(())λ₯Ό λΆ™μ—¬ ν˜ΈμΆœν•©λ‹ˆλ‹€.

μ΄λ•Œ κ΄„ν˜Έ μ•ˆμ—λŠ” ν•¨μˆ˜κ°€ ν•„μš”λ‘œ ν•˜λŠ” 인자(argument, μœ„ μ˜ˆμ‹œμ˜ "μ½”λ“œν”„λ Œμ¦ˆ")λ₯Ό μ „λ‹¬ν•©λ‹ˆλ‹€.


ν•¨μˆ˜ ν˜ΈμΆœν•˜κΈ°

κ΄„ν˜Έ(()) 없이 ν•¨μˆ˜ μ΄λ¦„λ§Œ μ‚¬μš©ν•˜λ©΄ ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜μ§€ μ•Šκ³ , ν•¨μˆ˜μ˜ μœ„μΉ˜λ₯Ό μ°Έμ‘°ν•˜κ²Œ λ©λ‹ˆλ‹€.

ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜λ €λ©΄ ν•¨μˆ˜ 이름 뒀에 λ°˜λ“œμ‹œ κ΄„ν˜Έλ₯Ό 뢙이고, κ΄„ν˜Έ μ•ˆμ—λŠ” ν•¨μˆ˜κ°€ ν•„μš”λ‘œ ν•˜λŠ” 인자λ₯Ό 전달해야 ν•©λ‹ˆλ‹€.

계산기 ν•¨μˆ˜ μ •μ˜
def calculator(number1, number2, operation): if operation == 'add': return number1 + number2 elif operation == 'subtract': return number1 - number2 elif operation == 'multiply': return number1 * number2 elif operation == 'divide': if number2 != 0: return number1 / number2 else: return "Division by zero error" else: return "Invalid operation" # ν•¨μˆ˜ 호좜 μ˜ˆμ‹œ result = calculator(10, 5, 'add') print("Addition:", result) result = calculator(10, 5, 'subtract') print("Subtraction:", result) result = calculator(10, 5, 'multiply') print("Multiplication:", result) result = calculator(10, 0, 'divide') print("Division:", result)

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result