Sep 19, 2019 12:36 AM
(This post was last modified: Sep 19, 2019 12:39 AM by Lindros.)
So basically found a program that calculates if a triangle is a right triangle using the Pythagorean theorem. Pretty simple, just uses manual user inputs but wanted to show the difference between me first learning and someone who is a competent engineer. You may get a kick out of this. Keep in mind I know there are probably better ways to do this than even these examples show, but rest assured both seem to work just fine.
Not only did the advanced code take less time for the person to write, it also looks better and uses less lines of code. The advanced code was done in approximately 2-3 minutes while basically scavenging the original code. Keep in mind for the purposes of my class I wasn't allowed to use functions and stuff because we were not at that point yet. It was only the first couple weeks of beginner python course.
Beginner code:
Code print("This program will determine if the triangle is a right triangle.")
side_a = float(input("Please enter the side one: "))
side_b = float(input("Please enter the side two: "))
side_c = float(input("Please enter the side three: "))
#Square the sides
side_a_sq = side_a ** 2
side_b_sq = side_b ** 2
side_c_sq = side_c ** 2
#Add the sides in pairs to check them against each side.
test_one = side_a_sq + side_b_sq
test_two = side_b_sq + side_c_sq
test_three = side_c_sq + side_a_sq
#Check the sum of 2 sides square equal any of the sides. Starting with side a. If not match is found after all three sides checked, it is not a right triangle.
if side_c_sq == test_one:
print("The triangle is a right triangle.")
elif side_a_sq == test_two:
print("The triangle is a right triangle.")
elif side_b_sq == test_three:
print("The triangle is a right triangle.")
else:
print("The triangle is not a right triangle.")
More Advanced code:
Code def get_input():
print("This program will determine if the triangle is a right triangle.")
side_a = float(input("Please enter the side one: "))
side_b = float(input("Please enter the side two: "))
side_c = float(input("Please enter the side three: "))
return sorted([side_a, side_b, side_c])
def is_triangle(sides):
return (sides[0]*sides[0] + sides[1]*sides[1]) == sides[2] * sides[2]
def main():
sides = get_input()
result = is_triangle(sides)
if result:
print("The triangle is a right triangle.")
else:
print("The triangle is a not right triangle.")
if __name__ == '__main__':
main()
Interesting post on this wonderful forum
Seems like you're getting the hang of it :)
You could add the following for an extra challenge:
- Error checking (thing try/except)
- Argparse, so it can be used as a command line tool
Nice work though
Skype messages are NOT valid evidence, and here's why.
Once you start making everything into methods your flow of writing code changes.
You break problems up into specific steps which logically do a different function.
At the end of the day though, as long as your code works!
(Sep 26, 2019 06:58 PM)iron_shocker Wrote: Once you start making everything into methods your flow of writing code changes.
You break problems up into specific steps which logically do a different function.
At the end of the day though, as long as your code works!
Yeah I actually have a bit of exposures to classes, functions and methods but for the purposes of this class we aren't aloud to do things we haven't talked about. I'm excited to advance more though.
The second piece of code is so much better to look at imo.
(Sep 27, 2019 01:13 PM)lilcsharp Wrote: The second piece of code is so much better to look at imo.
Yeah, that is the point lol. It is way better. Less lines, took less time to write. Uses the same logic and reusable functions, etc.
A very good post on a forum where 98% of the people are at the age where they find RAT's and keyloggers to be fascinating, however one problem with this post is that for tiny scripts like this, there is no need to write 10 functions, and an if name == main, because this script serves only one fucking purpose, and that's to get an area or whatever.
Great post, nevertheless, thank you for sharing. I learned quite a lot from it!
Slightly improved advanced code
Code import sys
from math import hypot
def is_ra_triangle(a: float, o: float, h: float) -> bool:
return hypot(a, o) == h
def init():
try:
sides = [float(input(f"Enter {side} => ")) for side in ("x", "y", "hypotenuse")]
except ValueError as e:
sys.exit(f"Invalid input, {e}")
if is_ra_triangle(*sides):
print("Right angled triangle")
else:
print("Other triangle", file=sys.stderr)
if __name__ == "__main__":
init()
(Sep 28, 2019 12:36 AM)incognito04 Wrote: Slightly improved advanced code
I'd have to read the documentation to see what you did there lol. Looks good though. Good job.
|