# Pythagoras v1 # Author: Raymond Yang # Date: 5/08/16 # Uses pythagoras theorem to solve equations, handles unexpected values. def yes_no(question): valid = False while not valid: response = input(question) if response.lower() == "yes" or response.lower() == "y": response = "yes" valid = True return response elif response.lower() == "no" or response.lower() == "n": response = "no" valid = True return response else: print("I don't understand, enter yes / no.") print() def numcheck(question, low): error = "Please enter a number that is more than {}".format(low) valid = False while not valid: try: response = float(input(question)) if response <= low: print(error) else: return response except ValueError: print(error) def numcheck_high(question, high): error = "Please enter a number that is less than {}".format(high) valid = False while not valid: try: response = float(input(question)) if response >= high: print(error) else: return response except ValueError: print(error) # main routine starts here keep_going = "yes" while keep_going == "yes": # gets info hyp_question = yes_no("Is the hypotenuse the missing side (y/n)?") if hyp_question.lower() == "yes" or hyp_question.lower() == "y": first_side_length = numcheck("What is the length of the first side?", 0) second_side_length = numcheck("What is the length of the second side?", 0) # does maths squared = first_side_length * first_side_length + second_side_length * second_side_length answer = squared ** 0.5 print("The hypotenuse side is {}".format(answer)) if hyp_question.lower() == "no" or hyp_question.lower() == "n": hyp_side_length = numcheck("What is the length of the hypotenuse?", 0) second_side_length = numcheck_high("What is the length of the second side?",hyp_side_length) # does maths squared = hyp_side_length * hyp_side_length - second_side_length * second_side_length answer = squared ** 0.5 print("The missing side is {}".format(answer)) # Loops program print() keep_going = yes_no("Do you want to keep going (yes / no) ? ") print() # code # first_side_length = float(input("What is the length of the first side?")) # second_side_length = float(input("What is the length of the second side?")) # hyp_side_length = float(input("What is the length of the hypotenuse?"))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question