判断三角形类型
import argparse def parser(): ap = argparse.ArgumentParser() ap.add_argument("-a", "--a", required=True, help="边a的边长") ap.add_argument("-b", "--b", required=True, help="边b的边长") ap.add_argument("-c", "--c", required=True, help="边c的边长") return ap.parse_args() def main(): opt = parser() a = float(opt.a) b = float(opt.b) c = float(opt.c) # a=input("边长a:") # b = inut("边长b:") if a+b>c and a+c>b and b+c>a: print ("a=%s, b=%s, c=%s 可以构成三角形" % (a, b, c)) if a == b == c: print ("a=%s, b=%s, c=%s 构成等边三角形" % (a, b, c)) elif a == b or b == c or a == c: print ("a=%s, b=%s, c=%s 构成等腰三角形" % (a, b, c)) if max([a, b, c]) ** 2 == min([a, b, c]) ** 2 * 2: print ("a=%s, b=%s, c=%s 构成直角三角形" % (a, b, c)) elif max([a, b, c]) ** 2 > min([a, b, c]) ** 2 * 2: print ("a=%s, b=%s, c=%s 构成钝角三角形" % (a, b, c)) elif max([a, b, c]) ** 2 < min([a, b, c]) ** 2 * 2: print ("a=%s, b=%s, c=%s 构成锐角三角形" % (a, b, c)) else: max_v = max([a, b, c]) min_v = min([a, b, c]) mid_v = list(set([a,b,c])-set([max(a,b,c),min([a,b,c])]))[0] if max_v**2 == min_v**2 + mid_v**2: print ("a=%s, b=%s, c=%s 构成直角三角形" % (a, b, c)) elif max_v**2 > min_v**2 + mid_v**2: print ("a=%s, b=%s, c=%s 构成钝角三角形" % (a, b, c)) else: print ("a=%s, b=%s, c=%s 构成锐角三角形" % (a, b, c)) else: print ("a=%s, b=%s, c=%s 不可以构成三角形" % (a, b, c)) return
main()