Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math #Import library that is able to perform math.sqrt()
- def isFloating(x):
- try:
- float(x) #If this can be executed, the number must be floating.
- return True #Therefore, return True.
- except:
- return False #If float(x) doesn't work, False is returned
- def calcC(a, b):
- c = math.sqrt(a**2 + b**2) #Mathematical implementation of core function a**2 + b**2 = c**2
- return c
- def calcAB(x, c):
- if c < x: #Test if the side is shorter than the inserted length.
- return "Erroneous input." #The side cannot be shorter than either length in a triangle.
- else:
- y = math.sqrt(c**2 - x**2) # Mathematical implementation of core function a**2 + b**2 = c**2
- return y
- def getValue(letter, unknown):
- while True: #Loop forever until "return" is called.
- value = input("What is the value of " + letter + "?: ") #User input
- if isFloating(value): #Test if the user inserted a correct value
- return float(value), unknown #Forward value
- elif value == "": #If nothing is inserted (""), it checks if it is the first unknown variable
- if unknown == 0:
- unknown = letter #If it's the first unknown variable, it assigns the variable "unknown" as the letter for later use.
- return None, unknown
- else:
- 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.
- elif unknown == 0:
- 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.
- else:
- print("You have to insert a numeric value.") #If nonsense is entered and there's no room for an unknown variable, this is given.
- def inputLetters():
- unknown = 0 #changes to a letter string so soon as one input is blank
- a, unknown = getValue("A", unknown)
- b, unknown = getValue("B", unknown)
- if unknown == 0:
- print(calcC(a, b))
- else:
- c, unknown = getValue("C", unknown)
- if unknown == "A":
- print(calcAB(b, c))
- else: #Or: if unknown == "B"
- print(calcAB(a, c))
- inputLetters()
Advertisement
Add Comment
Please, Sign In to add comment