##### ##def cfrac_n(f, n) : ## val = f(n) ## for i in range(n-1, -1, -1) : ## val = f(i) + 1/val ## return val ## ##def gen(n) : ## return 1 ## ##def gen1(n) : ## if n%3 == 0 : ## return 0 ## else: ## return 1 ## cfrac_n(gen1, 6) will cause an error ##def genS(n) : ## return 1 if n == 0 else 2 ## ##def genE(n) : ## if n == 0 : ## return 2 ## elif n%3 != 2 : ## return 1 ## return (n+1)//3 * 2 #### A program with bugs for debugging ##def is_pal(x): ## """assumes x is a 1ist ## returns True if x is a palindrome; False otherwise""" ## temp = x ## temp.reverse ## if temp == x: ## return True ## else: ## return False ## ##def judge(n): ## """assumes n is an int and n > 0 ## Gets n inputs from user ## Prints 'Yes' if input sequence forms a palindrome; ## 'No' otherwise""" ## for i in range(n): ## result = [] ## elem = input('Enter item: ') ## result.append(elem) ## if isPal(result): ## print("Yes") ## else: ## print("No") ## Code for requiring an integer ##while True: ## try: ## x = int(input("Please enter an integer: ")) ## break ## except ValueError: ## print("Error! Input was not a valid integer. Try again ... ") ## Code for requiring name of a data file ##while True : ## fname = input("Please give the file name: ") ## try : ## infile = open(fname) ## break ## except OSError : ## print("Cannot open " + fname + ".", "Another...") ## ##infile.close() ##def inputInt(): ## """Function for input an integer""" ## while True: ## try: ## x = int(input("Please enter an integer: ")) ## return x ## except ValueError: ## print("Error! Input was not a valid integer." ## "Try again ... ") ##def inputVal(valType, requestMsg, tpName): ## """Generic function for input a value of a given type.""" ## while True: ## val = input(requestMsg + " ") ## try: ## val = valType(val) ## return val ## except ValueError: ## print(val + " is not of type " + tpName + ".", ## "Try again...") ##def getDataFileName (): ## while True : ## fname = input("Please give the file name: ") ## try : ## infile = open(fname) ## return infile ## except OSError : ## print("Cannot open " + fname + ".", "Another...") from math import sqrt def triangle(a, b, c) : if a > 0 and b > 0 and c > 0 \ and a+b > c and a+c > b and b+c > a : s = (a + b + c) / 2 return sqrt(s * (s - a) * (s - b) * (s - c)) else: raise ValueError("wrong argument(s) for function triangle") def testTri () : try : print(triangle(3, 5, 6)) print(triangle(2, 4, 7)) except ValueError : print("Find error.") def testTri1 () : try : print(triangle(3, 5, 6)) print(triangle(2, 4, 7)) except ValueError as msg : print("Find error:", msg) def triangle(a, b, c) : if a > 0 and b > 0 and c > 0 \ and a+b > c and a+c > b and b+c > a : s = (a + b + c) / 2 return sqrt(s * (s - a) * (s - b) * (s - c)) else: raise ValueError("wrong argument(s) for function triangle", (a, b, c))