Rubykuby

a^2 + b^2 = c^2

Sep 9th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. import math #Import library that is able to perform math.sqrt()
  2.  
  3. def isFloating(x):
  4.     try:
  5.         float(x) #If this can be executed, the number must be floating.
  6.         return True #Therefore, return True.
  7.     except:
  8.         return False #If float(x) doesn't work, False is returned
  9.  
  10. def calcC(a, b):
  11.     c = math.sqrt(a**2 + b**2) #Mathematical implementation of core function a**2 + b**2 = c**2
  12.     return c
  13.  
  14. def calcAB(x, c):
  15.     if c < x: #Test if the side is shorter than the inserted length.
  16.         return "Erroneous input." #The side cannot be shorter than either length in a triangle.
  17.     else:
  18.         y = math.sqrt(c**2 - x**2) # Mathematical implementation of core function a**2 + b**2 = c**2
  19.         return y
  20.  
  21. def getValue(letter, unknown):
  22.     while True: #Loop forever until "return" is called.
  23.         value = input("What is the value of " + letter + "?: ") #User input
  24.         if isFloating(value): #Test if the user inserted a correct value
  25.             return float(value), unknown #Forward value
  26.         elif value == "": #If nothing is inserted (""), it checks if it is the first unknown variable
  27.             if unknown == 0:
  28.                 unknown = letter #If it's the first unknown variable, it assigns the variable "unknown" as the letter for later use.
  29.                 return None, unknown
  30.             else:
  31.                 print("You have to insert a numeric value.") #If "unknown" already has a value, it tells the user that a numeric value MUST be inserted.
  32.         elif unknown == 0:
  33.             print("Leave blank if you don't have a value.") #If a non-blank non-floating entry was added, it'll display this so long as there's room for a blank entry.
  34.         else:
  35.             print("You have to insert a numeric value.") #If nonsense is entered and there's no room for an unknown variable, this is given.
  36.  
  37. def inputLetters():
  38.     unknown = 0 #changes to a letter string so soon as one input is blank
  39.     a, unknown = getValue("A", unknown)
  40.     b, unknown = getValue("B", unknown)
  41.     if unknown == 0:
  42.         print(calcC(a, b))
  43.     else:
  44.         c, unknown = getValue("C", unknown)
  45.         if unknown == "A":
  46.             print(calcAB(b, c))
  47.         else: #Or: if unknown == "B"
  48.             print(calcAB(a, c))
  49.        
  50. inputLetters()
Advertisement
Add Comment
Please, Sign In to add comment