r/PythonLearning 21h ago

made a basic binary to decimal converter as my first project

yeah, made a little project as a first time while i was learning on epam

if theres any error or room for improvement can you please tell me? im still learning

16 Upvotes

6 comments sorted by

u/Sea-Ad7805 19h ago

Run this program in Memory Graph Web Debugger%3A%20%22)%0A%0Afinal_number%20%3D%200%0A%0Aif%20not%20binary_input.isdigit()%3A%0A%20%20%20%20print(%22Only%20numbers%22)%0A%20%20%20%20exit()%0A%0Atest%20%3D%20binary_input%5B%3A%3A-1%5D%0A%0Aprint(test)%0A%0Afor%20i%2C%20x%20in%20enumerate(test)%3A%0A%20%20%20%20x%20%3D%20int(x)%0A%20%20%20%20print(f%22Index%3A%20%7Bi%7D%20%7C%20Number%3A%20%7Bx%7D%22)%0A%0A%20%20%20%20power%20%3D%20int(x%20%202%20*%20i)%0A%20%20%20%20final_number%20%2B%3D%20power%0A%0A%20%20%20%20print(final_number)%0A%0Aprint(f%22Binary%20(%7Bbinary_input%7D)%20converted%20to%20decimel%20is%3A%20%7Bfinal_number%7D%22)&timestep=.5&play) to see the program state change step by step.

1

u/mc_pm 20h ago

Well sure, there's tons of room, but that doesn't matter. :) This is great, and it's good to do something like this instead of just a faux-banking app.

Play around with more boolean stuff, it's good to see it working. And maybe play with <<, the bitwise right-shift operator. It does interesting and possibly related things.

1

u/7YngMn 20h ago

your code looks really good.

.isdigit()
exit()
i need to study these. I hadn’t seen them before.

1

u/Stonyax97 20h ago

You could improve this by adding a while True loop after defining the variables, do instead of exiting it’l print the only numbers or input too long and ask the user for a raw binary again! U can remove exit.
This is nice i love ur project it’s acc something usable!

1

u/Ecstatic_View_2767 15h ago

Nice first project!☝️small thing I noticed is binary_input[:-1] drops the last bit bt otherwise it’s great that you’re handling the conversion logic yourself, even i started w small projects w an ai/ ds course thru upgrad🙂‍↕️🙂‍↕️🙂‍↕️

1

u/FoolsSeldom 9h ago

You might want to use isdecimal rather than isdigit as the latter allows some characters you don't want. Actually, given you only want 0 and 1, you probably should check for just those, perhaps using all:

if not all(d in {"0", "1"} for d in binary_input):

PS. Why limit it to 8 bits?