Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
This is really cool! It's neat that you're doing this with your daughters. Hopefully, they liked it!
Since it's pretty much a straightforward one-off calculation, I don't think there's a whole lot of optimizing you can do. I do have a couple "this is how you'll see it a lot in Python code" kind of things, though.
Generally, in a Python script like this where your interface (argument processing, getting user input, etc.) is in the same file as your main logic, you'll see bottom section written like this:
if__name__=='__main__':iflen(sys.argv)!=4:print('\nNEED 3 VALUES IN THE INPUT!!\n')exit(1)distance=float(sys.argv[1])radius1=float(sys.argv[2])radius2=float(sys.argv[3])ifdistance<0orradius1<0orradius2<0:print('Values cannot be negative. Goodbye')exit(1)area=calcAreaIntersectingCircles(distance,radius1,radius2)print('\nThe intersecting area of the two circles is',round(area,2),'square units\n')
Python modules get a __name__ attribute when they're run. If they're called directly (a la python gimme-that-area.py), the __name__ attribute is set to __main__. If you import them, (like import gimme-that-area), the __name__ is set to "gimme-that-area". So this way, it allows you to import your code and use your calcAreaIntersectingCircles function in other places without erroring out due to missing command line args.
Other than that, if you want to get a feel for traditional Python style, you should have a read through PEP8, the main part of Python's style guide. You can run your code through this checker for some automatic feedback. Once you get comfortable installing packages with pip, try out pep8 or, my favorite, flake8.
If you ever have any questions about Python, feel free to reach out to me. I'm always happy to answer questions or help out.
This is really cool! It's neat that you're doing this with your daughters. Hopefully, they liked it!
Since it's pretty much a straightforward one-off calculation, I don't think there's a whole lot of optimizing you can do. I do have a couple "this is how you'll see it a lot in Python code" kind of things, though.
Generally, in a Python script like this where your interface (argument processing, getting user input, etc.) is in the same file as your main logic, you'll see bottom section written like this:
Python modules get a
__name__
attribute when they're run. If they're called directly (a lapython gimme-that-area.py
), the__name__
attribute is set to__main__
. If you import them, (likeimport gimme-that-area
), the__name__
is set to "gimme-that-area". So this way, it allows you to import your code and use yourcalcAreaIntersectingCircles
function in other places without erroring out due to missing command line args.Other than that, if you want to get a feel for traditional Python style, you should have a read through PEP8, the main part of Python's style guide. You can run your code through this checker for some automatic feedback. Once you get comfortable installing packages with
pip
, try out pep8 or, my favorite, flake8.If you ever have any questions about Python, feel free to reach out to me. I'm always happy to answer questions or help out.
Very helpful. Will check out the style guide. And will definitely reach out for any questions. Thank you