Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Beginner Student vs Software Engineer Python 3 Code

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:
Spoiler (Click to View)


More Advanced code:
Spoiler (Click to View)
[Image: Iu0EfTL.gif]
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.
[Image: Iu0EfTL.gif]
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.
[Image: Iu0EfTL.gif]
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.
[Image: Iu0EfTL.gif]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by VitaniAug 20, 2023 01:46 AM 290 29,366 Aug 20, 2023 01:46 AM
Last Post: Lucky4L
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 630 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[Python] by Empery_1GAug 20, 2023 01:33 AM 2 226 Aug 20, 2023 01:33 AM
Last Post: Lucky4L