編寫一個 Python 程序,使用用戶指定的角度檢查三角形是否有效。記住,任何三角形都是有效的,如果三角形中 3 個角的和等於 180°
檢查三角形是否有效的 Python 程序示例 1
這個 python 程序幫助用戶輸入三角形的所有角度。接下來,我們使用 If Else 語句檢查給定角度之和是否等於 180°。如果為真,print 語句將列印一個有效的三角形。否則, python 程序列印為無效三角形。
# Python Program to check Triangle is Valid or Not
a = int(input('Please Enter the First Angle of a Triangle: '))
b = int(input('Please Enter the Second Angle of a Triangle: '))
c = int(input('Please Enter the Third Angle of a Triangle: '))
# checking Triangle is Valid or Not
total = a + b + c
if total == 180:
print("\nThis is a Valid Triangle")
else:
print("\nThis is an Invalid Triangle")
驗證三角形是否有效的 Python 程序示例 2
在上面的 Python 例子中,我們忘了檢查任何一個角度是否為零。因此,我們使用邏輯與運算符來確保所有角度都大於 0
a = int(input('Please Enter the First Angle of a Triangle: '))
b = int(input('Please Enter the Second Angle of a Triangle: '))
c = int(input('Please Enter the Third Angle of a Triangle: '))
# checking Triangle is Valid or Not
total = a + b + c
if (total == 180 and a != 0 and b != 0 and c != 0):
print("\nThis is a Valid Triangle")
else:
print("\nThis is an Invalid Triangle")
Please Enter the First Angle of a Triangle: 70
Please Enter the Second Angle of a Triangle: 70
Please Enter the Third Angle of a Triangle: 40
This is a Valid Triangle
>>>
=================== RESTART: /Users/suresh/Desktop/simple.py ===================
Please Enter the First Angle of a Triangle: 90
Please Enter the Second Angle of a Triangle: 90
Please Enter the Third Angle of a Triangle: 0
This is an Invalid Triangle
>>>
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/301410.html