编写一个 Python 程序,使用用户指定的角度来检查三角形是否有效。请记住,任何三角形只要三条角之和等于 180 度,则该三角形就是有效的。
Python 程序:检查三角形是否有效 示例 1
这个 Python 程序帮助用户输入三角形的所有角度。接下来,我们使用 If Else 语句 来检查给定角度的总和是否等于 180。如果为 True,则打印语句打印“这是一个有效三角形”。否则,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 示例中,我们忘了检查是否有任何角度为零。因此,我们使用了 逻辑 AND 运算符来确保所有角度都大于 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
>>>