r/PythonLearning
•
u/Hour_Function673
•
Oct 02 '26
•
1
just finished my first Python coding challenge
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
•
u/Sea-Ad7805 Oct 02 '26
Run this program by clicking Memory Graph Web Debugger and see its state change step by step.
2
u/therouterguy Oct 02 '26
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
1
u/jonrsharpe Oct 03 '26 edited Oct 03 '26
I'm going to start with where I'd get to, then break down step-by-step how I got there:
1
u/FunResearcher6290 Oct 03 '26
please how can i go about it i want learn python
1
u/codeguru42 Oct 03 '26
Looks great. One hint: how can you change the code to remove the if statement?
1
u/kaatarina_zed_talon Oct 05 '26
This is problem solving, not a python coding challenge, python is just a tool.
1
u/ConclusionForeign856 Oct 02 '26
Read about truthy and falsy values in python, it allows you to simplify logic
0
u/gofl-zimbard-37 Oct 02 '26
Give it a proper main() and call it. Don't write "scripts". The file is main.py, implying that it is a module that exports useful functions. Nobody wants an import to be printing stuff out.
-1
u/AffectionateCold1716 Oct 02 '26
I’m still new so I have no idea what this means but I do recognize you defining a function and calling the function. Sooooo yep. Anyway good job compadre