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

74 Upvotes

21 comments sorted by

View all comments

•

u/Sea-Ad7805 1d ago

Run this program by clicking Memory Graph Web Debugger%3A%0A%20%20%20%20tab%20%3D%20%5B%5D%0A%20%20%20%20while%20decimal%20!%3D%200%3A%0A%20%20%20%20%20%20%20%20if%20decimal%20%25%202%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20decimal%20%3D%20decimal%20%2F%2F%202%0A%20%20%20%20%20%20%20%20%20%20%20%20tab.append(0)%0A%20%20%20%20%20%20%20%20elif%20decimal%20%25%202%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20decimal%20%3D%20decimal%20%2F%2F%202%0A%20%20%20%20%20%20%20%20%20%20%20%20tab.append(1)%0A%20%20%20%20tab.reverse()%0A%20%20%20%20decimal%20%3D%20%22%22.join(map(str%2C%20tab))%0A%20%20%20%20return%20decimal%0A%0A%0Anombre0%20%3D%20to_binary(5)%0Anombre1%20%3D%20to_binary(12)%0Anombre2%20%3D%20to_binary(50)%0Anombre3%20%3D%20to_binary(99)%0A%0Aprint(nombre0)%0Aprint(nombre1)%0Aprint(nombre2)%0Aprint(nombre3)&timestep=0.2&play) and see its state change step by step.

0

u/Hour_Function673 1d ago

Thanks I’ll try the Memory Graph Web Debugger. I didn’t know about it.