คำนวณพื้นที่ลูกบาศก์โดยภาษา Python

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

ichimarusoichi
PHP Super Member
PHP Super Member
โพสต์: 352
ลงทะเบียนเมื่อ: 07/01/2019 12:19 pm

คำนวณพื้นที่ลูกบาศก์โดยภาษา Python

โพสต์ที่ยังไม่ได้อ่าน โดย ichimarusoichi »

วันนี้ผมจะมานำเสนอการใช้ Python มาคำนวณพื้นลูกบาศก์โดยมีหน่วยเป็นเซนติเมตรขั้นตอนในการทำงานนั้นจะในคำสั่งของ Labelและ textboxลองทำความเข้าใจในคำสั่งและลองเขียนได้เลยครับ

Input

โค้ด: เลือกทั้งหมด

from guizero import App, Text, TextBox, PushButton, error

def calculate():
    # ตรวจสอบชุดข้อมูลที่กรอกต้องเป็นตัวเลข
    if not height.value.isdigit() or not width.value.isdigit():
        error("Input error", "You must type in numbers for height and width")
    # ในส่วนของความลึกนั้นไม่ต้องก็ได้
    elif not depth.value.isdigit() and depth.value != "":
        error("Input error", "You must type in a number for depth")

    #คำนวณพื้นที่ลูกบาศก์
    else:
        area = int(height.value) * int(width.value)
        if depth.value == "":
            result.value = str(area) + "cm squared"
        else:
            volume = area * int(depth.value)
            result.value = str(volume) + "cm cubed"

#กำหนดแบบฟอร์มหน้าจอการทำงาน
app = App("เครื่องช่วยคำนวณหาพื้นที่ลูกบาศก์", layout="grid")

width_label = Text(app, text="Width:", grid=[0, 0], align="left")
width = TextBox(app, grid=[1, 0], align="left", width=30)

height_label = Text(app, text="Height:", grid=[0, 1], align="left")
height = TextBox(app, grid=[1, 1], align="left", width=30)

depth_label = Text(app, text="Depth:", grid=[0, 2], align="left")
depth = TextBox(app, grid=[1, 2], align="left", width=30)

button = PushButton(app, calculate, text="Calculate", grid=[1, 3])

result_label = Text(app, text="Result:", grid=[0, 4], align="left")
result = TextBox(app, grid=[1, 4], align="left", width=30)

app.display()
Output
หน้าจอเครื่องช่วยคำนวณพื้นที่ลูกบาศก์
หน้าจอเครื่องช่วยคำนวณพื้นที่ลูกบาศก์
areabox.PNG (4.52 KiB) Viewed 2053 times
ช่อง Width ให้ใส่ความกว้าง
ช่อง Height ให้ใส่ความสูง
ช่อง Depth ให้ใส่ความลึก
จากนั้นกดปุ่ม Calculate
ผลลัพธ์ที่ได้จะแสดงในช่อง Result

ในส่วนคำสั่งการทำงานนั้นจะต้องทำการช่อง Width Height Depth Result ด้วยคำสั่งtextbox ส่วนชื่อที่อยู่ด้านหน้าให้สร้างเป็น Label โดยคำนวณตัวเลขโดยกดปุ่ม Calculate เพื่อคำนวณผลลัพธ์ออกมา และการทำคำนวณหาค่าพื้นที่ลูกบาศก์นั้นจะต้องใส่ตัวเลขเท่านั้นแต่ Depth ความลึกนั้นไม่ต้องใส่ก็ได้

ช่องทางการศึกษาเพิ่มเติมข่าวที่น่าสนใจเกี่ยวกับ : คำนวณพื้นที่ลูกบาศก์โดยภาษา Python
-บทเรียน Python
-บทเรียน Python Framework Flask
-ความหมายของคำ คืออะไร
-VDO Tutorial - Python
-ถาม-ตอบ Java & Python
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 96