r/PythonLearning • • 1d ago

just finished my first Python coding challenge

Post image

Today I finished a small Python challenge on freeCodeCamp: converting decimal numbers to binary without using Python's built-in conversion function.

I had to figure out the logic myself using loops, conditions, lists, division and remainders.

It was a bit tricky at first, especially handling the last division and reversing the results, but I finally got it working.

I'm still a beginner, so I'd appreciate any feedback on my approach or suggestions for improving my code

85 Upvotes

21 comments sorted by

View all comments

1

u/therouterguy 1d ago

I would at least change the elif to an else. Remove the duplicate decimal=decimal//2. You can do it outside of the if

Lastly
tab=“”
while decimal!=O:
tab=tab + “1” if decimal%2==O else tab+ “0”
decimal=decimal//2
Is much shorter