본문 바로가기
개념/혼자 공부하는 파이썬

python) how to use the module package in python?

by kiseno 2024. 11. 18.
728x90
반응형
SMALL

A_0

PI = 3.141592
def number_input():
    output = input("숫자 입력 : ")
    return float(output)

def get_circumference(radius):
    return 2 * PI * radius

def get_circle_area(radius):
    return PI * radius * radius

1. **PI = 3.141592**: Sets a constant named `PI` to an approximation of π, a mathematical constant that represents the ratio of a circle's circumference to its diameter.

2. **def number_input():** Defines a function `number_input` that prompts the user for a number input.
   - **output = input("숫자 입력 : "):** This line displays the prompt "숫자 입력 : " (which translates to "Enter a number: ") and captures the user's input as a string.
   - **return float(output):** Converts the string input to a floating-point number and returns this number. This is necessary for mathematical calculations that follow.

3. **def get_circumference(radius):** Defines a function to calculate the circumference of a circle given its radius.
   - **return 2 * PI * radius:** Uses the formula for the circumference of a circle, \(C = 2\pi r\), where \(r\) is the radius. The result is returned as the function's output.

4. **def get_circle_area(radius):** Defines a function to calculate the area of a circle given its radius.
   - **return PI * radius * radius:** Uses the formula for the area of a circle, \(A = \pi r^2\), where \(r\) is the radius. The result is returned as the function's output.

This script is structured to demonstrate the use of functions for organizing code in Python, making it modular and reusable. The `number_input` function is particularly useful for any calculation requiring numeric input from the user, while `get_circumference` and `get_circle_area` apply to geometric calculations involving circles. Although the snippet defines these functions, it doesn't include code to invoke them, which would typically be done in a more comprehensive script or during an interactive Python session.

 

A_1

import module_A_0 as test

radius = test.number_input()
print(test.get_circumference(radius))
print(test.get_circle_area(radius))

1. **import module_A_0 as test**: This line imports the module named `module_A_0` and gives it an alias `test` for ease of reference. This means that the functions defined in `module_A_0` can now be accessed using the prefix `test.`.

2. **radius = test.number_input()**: Executes the `number_input` function defined in the `module_A_0` module. This function prompts the user to enter a number, which it expects to be the radius of a circle. The input is then converted to a floating-point number and stored in the variable `radius`.

3. **print(test.get_circumference(radius))**: Calls the `get_circumference` function from `module_A_0` using the `radius` obtained from the user. This function calculates the circumference of a circle using the formula \(2\pi r\) and prints the result.

4. **print(test.get_circle_area(radius))**: Calls the `get_circle_area` function from `module_A_0` using the `radius` obtained from the user. This function calculates the area of a circle using the formula \(\pi r^2\) and prints the result.

728x90
반응형
LIST