Solving one side of right-angled triangle using Pythagorean Theorem in Python

in utopian-io •  7 years ago  (edited)

the-pythagorean-formula.png
image source

In a right-angled triangle:
the square of the hypotenuse is equal to
the sum of the squares of the other two sides.

What Will I Learn?

This tutorial aims to:

  • Solve one side of a right-angled triangle using Pythagorean Theorem in Python

Requirements

  • Python IDLE

Difficulty

  • Basic

Tutorial Contents

The Code:

First, we import sqrt function from math module into the current namespace.
from math import sqrt

Display an information about the program (may not be included).
print('This is a Pythagorean Theorem calculator! Calculate one side of right-angled triangle.')
print('Assume the sides are a, b, c and c is the hypotenuse.')

Second, ask the user on which side to calculate and assign it to a variable (i used variable 'choice').
choice = input('Which side (a, b, c) do you want to calculate? side: ')

Third, making if ... elseif condition statement.
If the user wants to find for the hypotenuse which is side c.
if choice == 'c':
a = float(input('Input the length of side a: '))
b = float(input('Input the length of side b: '))

Solving for the c with the formula: √a^2+b^2
c = sqrt(a * a + b * b)
Display the value of side c
print('The length of side c is %g: ' %(c))

Else if, the user wants to find for the side b.
elif choice == 'b':
a = float(input('Input the length of side a: '))
c = float(input('Input the length of side c: '))

Solving for the b with the formula: √c^2-a^2
b = sqrt(c * c - a * a)
Display the value of side b
print('The length of side b is %g: ' %(b))

Else if, the user wants to find for the side a.
elif choice == 'a':
b = float(input('Input the length of side b: '))
c = float(input('Input the length of side c: '))

Solving for the a with the formula: √c^2-b^2
a = sqrt((c * c) - (b * b))
Display the value of side a
print('The length of side a is %g: ' %(a))

Lastly, if the input is invalid.
else:
print('Invalid Input!')

Curriculum

Please visit my previous tutorials:

Thanks for reading! Hope you learn something.



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Your contribution cannot be approved because it is not as informative as other contributions. See the Utopian Rules. Contributions need to be informative and descriptive in order to help readers and developers understand them.

Explanation:

  • the GitHub repo you linked to, is 9 months old, probably is written by somebody else, and does the exact same thing as you are trying to teach. (Only your code is less elegant). I won't call it "plagiarism" but it's not original either!
  • your tutorial doesn't provide the full Python3 script,
  • if you want to contribute more Python3 tutorials - you're welcome!- but at least then try to explain in a step-by-step fashion how the Python 3 language works, or explain how to implement / use a certain module (included in the standard lib, or external), but do not publish tutorials for very trivial examples: now you picked the Pythagorean Theorem (c^2 = a^2 + b^2), and I don't think any serious Python developer is interested in a tutorial to solve that Theorem,
  • but any beginner Pythonista is interested in good tutorials explaining language and/or module concepts.

Good luck on your next Python3 tutorial!

You can contact us on Discord.
[utopian-moderator]

thank you sir

Hey @scipio, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!