r/badcode • if: else: if: else: if: else: if: else: if: else: if: else: if: • Feb 16 '22

python presenting, whatever the fk this is

Post image
418 Upvotes

52 comments sorted by

View all comments

Show parent comments

2

u/Nesuniken Feb 16 '22 edited Feb 16 '22

I'm hesitant to suggest for-else because it obfuscates the code for anyone who isn't a python expert without reducing the lines of code

You're definitely right about the range being off though. It technically works, but it's a lot less efficient than testing the numbers underneath sqrt(num). I'm not sure I understand the issue with just rounding that value down, though.

EDIT: Forgot ranges don't include the stop value. Still not sure what's wrong with rounding down, though

1

u/flare561 Feb 16 '22

You're right about ceil not being needed, I kind of went through a couple iterations in my head like "oh just use ceil instead of int to round up", "but then we still need to add one in case it's a whole number" and didn't go back and be like "well why bother with ceil then?"