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
if __name__ == '__module_C_1__':
print("get_circumference(10) : ", get_circumference(10))
print("get_circle_area(10) : ", get_circle_area(10))
1. **PI = 3.141592**: Defines a constant `PI` that holds the approximation of π, a mathematical constant used in the calculation of circumference and area of a circle.
2. **def number_input():** Defines a function named `number_input` that prompts the user to input a number. This function is designed to:
- Display a prompt "숫자 입력 : " (translates to "Enter a number: ") to the user.
- Capture the user's input, convert it to a floating-point number using `float(output)`, and return this number.
3. **def get_circumference(radius):** Defines a function to calculate the circumference of a circle given its radius.
- Utilizes the formula for circumference \(C = 2\pi r\), calculates it by `return 2 * PI * radius`, and returns the result.
4. **def get_circle_area(radius):** Defines a function to calculate the area of a circle given its radius.
- Uses the area formula \(A = \pi r^2\), calculates it by `return PI * radius * radius`, and returns the computed area.
5. **if __name__ == '__module_C_1__':** This conditional block checks if the script is being run as a standalone program named `__module_C_1__`. The `__name__` variable in Python is a special built-in variable which evaluates to the name of the current module. However, when a Python script is executed directly (not imported as a module in another script), `__name__` is set to the string `'__main__'`.
- Since the condition checks for `__name__ == '__module_C_1__'` instead of the usual `'__main__'`, this block of code will not execute under normal circumstances where the script is run directly. It would only execute if somehow the `__name__` variable is manually set to `'__module_C_1__'` or if this script is being used in a very unconventional way that sets `__name__` to `'__module_C_1__'`.
- Inside this conditional block, it prints the circumference and area of a circle with a radius of 10 by calling `get_circumference(10)` and `get_circle_area(10)` respectively, showcasing the use of the previously defined functions.
A_1
import module_C_0 as test
radius = test.number_input()
print(test.get_circumference(radius))
print(test.get_circle_area(radius))
1. **import module_C_0 as test**: This line imports the module named `module_C_0`, allowing access to its functions and variables under the alias `test`. This practice helps keep code organized and avoids naming conflicts.
2. **radius = test.number_input()**: Calls the `number_input()` function from the imported module. This function is expected to prompt the user for a numerical input, interpret it as the radius of a circle, and return it as a floating-point number. The returned value is stored in the variable `radius`.
3. **print(test.get_circumference(radius))**: Invokes the `get_circumference()` function from the `test` module, passing the previously obtained `radius` as an argument. This function calculates the circumference of a circle using the provided radius and returns the result. The script then prints this value, displaying the circumference to the user.
4. **print(test.get_circle_area(radius))**: Similar to the previous step, this line calls the `get_circle_area()` function from the `test` module, again using the `radius` as its argument. This function calculates the area of a circle based on the given radius and returns the result, which is then printed out. This shows the circle's area to the user.
'개념 > 혼자 공부하는 파이썬' 카테고리의 다른 글
python) __name__ method (Calculating Circumference and Area of a Circle in Python) (0) | 2024.11.19 |
---|---|
python) how to use the module package in python? (0) | 2024.11.18 |
python) making custom module packages in python (0) | 2024.11.16 |
python) Counting Loop Iterations in a 5-Second Interval. (0) | 2024.11.15 |
python) Circle Class with Encapsulation and Property Decorators. (3) | 2024.11.14 |