Latest web development tutorials

Python menghitung luas segitiga

Dokumen Referensi Object contoh Python3

Contoh berikut adalah sisi segitiga dengan panjang input pengguna, dan menghitung luas segitiga:

# -*- coding: UTF-8 -*-

# Filename : test.py
# author by : www.w3big.com


a = float(input('输入三角形第一边长: '))
b = float(input('输入三角形第二边长: '))
c = float(input('输入三角形第三边长: '))

# 计算半周长
s = (a + b + c) / 2

# 计算面积
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('三角形面积为 %0.2f' %area)

Mengeksekusi hasil kode output di atas:

$ python test.py 
输入三角形第一边长: 5
输入三角形第二边长: 6
输入三角形第三边长: 7
三角形面积为 14.70

Dokumen Referensi Object contoh Python3