HW02 運算子表達式 115103205
2. Youtube
3.學習卡與測驗
測驗
4.程式碼
1. Python 程式碼
檔案名稱:sphere_volume.py
# Program: Calculate the Volume of a Sphere# Formula: Volume = (4/3) * 3.14 * radius^3radius = float(input("Enter the radius of Sphere: "))volume = (4 / 3) * 3.14 * radius ** 3print("Radius of Sphere:", radius)print("Volume of Sphere is: %.2f" % volume)2. 執行結果(Output)
Enter the radius of Sphere: 5
Radius of Sphere: 5.0
Volume of Sphere is: 523.333. 程式說明(Program Explanation)
程式碼 | 說明 |
|---|---|
| 讓使用者輸入球體半徑 |
| 將輸入資料轉換成浮點數 |
| 計算半徑的三次方 |
| 計算球體體積 |
| 輸出計算結果 |
| 將體積顯示到小數點後兩位 |
計算公式:
4. GitHub README.md
以下內容可以直接複製到 GitHub 的 README.md 檔案。
Sphere Volume Calculator
Introduction
This project is a simple Python program that calculates the volume of a sphere based on the radius entered by the user.
Formula
The formula for calculating the volume of a sphere is:
[
V = \frac{4}{3} \times 3.14 \times r^3
]
Where:
Vrepresents the volume of the sphere.rrepresents the radius of the sphere.3.14is the approximate value of pi.
Requirements
Python 3.x
Source Code
radius = float(input("Enter the radius of Sphere: "))
volume = (4 / 3) * 3.14 * radius ** 3
print("Radius of Sphere:", radius)
print("Volume of Sphere is: %.2f" % volume)Example Output
Enter the radius of Sphere: 5
Radius of Sphere: 5.0
Volume of Sphere is: 523.33How to Run
Install Python 3.
Download or clone this repository.
Open a terminal in the project folder.
Run the following command:
python sphere_volume.pyEnter the radius when prompted.
The program will display the radius and the calculated volume.
Learning Objectives
Learn how to receive user input using
input().Understand type conversion using
float().Practice arithmetic operations in Python.
Learn how to format output to two decimal places.
License
This project is available for educational and learning purposes.
5. GitHub 專案檔案配置
建議建立以下結構:
sphere-volume-calculator/
├── sphere_volume.py
└── README.mdsphere_volume.py 放程式碼,README.md 放專案介紹、公式、執行結果與操作說明。
這樣就完成一個適合初學者學習、也可以上傳到 GitHub 作為 Python 練習作品的專案。










留言
張貼留言