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

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: คำนวณพื้นที่ลูกบาศก์โดยภาษา Python

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

โดย ichimarusoichi » 04/02/2019 6:47 pm

วันนี้ผมจะมานำเสนอการใช้ 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 2048 times
ช่อง Width ให้ใส่ความกว้าง
ช่อง Height ให้ใส่ความสูง
ช่อง Depth ให้ใส่ความลึก
จากนั้นกดปุ่ม Calculate
ผลลัพธ์ที่ได้จะแสดงในช่อง Result

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

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

ข้างบน