HW02 運算子表達式 115103205

 1.NoteBook LM


 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.33

3. 程式說明(Program Explanation)

程式碼

說明

input()

讓使用者輸入球體半徑

float()

將輸入資料轉換成浮點數

radius ** 3

計算半徑的三次方

(4 / 3) * 3.14 * radius ** 3

計算球體體積

print()

輸出計算結果

%.2f

將體積顯示到小數點後兩位

計算公式:

\(V = \frac{4}{3}\pi r^3\)
\(V=\frac{4}{3}\pi(\text{5.9})^3\approx \text{860.29}\)
\(r\)
r = 5.9
提供回饋

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:

  • V represents the volume of the sphere.

  • r represents the radius of the sphere.

  • 3.14 is 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.33

How to Run

  1. Install Python 3.

  2. Download or clone this repository.

  3. Open a terminal in the project folder.

  4. Run the following command:

python sphere_volume.py
  1. Enter the radius when prompted.

  2. 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.md

sphere_volume.py 放程式碼,README.md 放專案介紹、公式、執行結果與操作說明。

這樣就完成一個適合初學者學習、也可以上傳到 GitHub 作為 Python 練習作品的專案。

 

留言

這個網誌中的熱門文章

HW01 Python 環境建立和Hello World

HW03 判斷敘述式