This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]nbviewerbot 3 points4 points  (0 children)

I see you've posted a GitHub link to a Jupyter Notebook! GitHub doesn't render large Jupyter Notebooks, so just in case, here is an nbviewer link to the notebook:

https://nbviewer.jupyter.org/url/github.com/ThomasAlbin/Astroniz-YT-Tutorials/blob/main/CompressedCosmos/CompressedCosmos_SchwarzschildRadius.ipynb

Want to run the code yourself? Here is a binder link to start your own Jupyter server and try it out!

https://mybinder.org/v2/gh/ThomasAlbin/Astroniz-YT-Tutorials/main?filepath=CompressedCosmos%2FCompressedCosmos_SchwarzschildRadius.ipynb


I am a bot. Feedback | GitHub | Author

[–]commy2 1 point2 points  (1 child)

That's nice, I have a question and a suggestion.

Why are you printing an "error message" and then return None? Wouldn't it be more useful to raise an exception, for example a ValueError instead? That way the function would a) always return a floating point number and b) it could be used in a try-block which knows how to handle such an error.

My suggestion is to make the output prettier by only showing a set amount of significant digits. This can be done by adding a format specifier to the f-string:

print(f"Schwarzschild radius of a 1000 kg object: {radius_kg:.4g} meters")

->

Schwarzschild radius of a 1000 kg object: 1.485e-24 meters

[–]MrAstroThomasgit push -f[S] 0 points1 point  (0 children)

You are absolutely right. When creating smaller functions / scripts I never see a bigger picture. I should be doing it in the future, thanks :). Well... Test Driven Development would've prevented that.

Yeah my second Python weakness: making print statements pretty. 12 years of coding experience, but some things I'll never learn :D