(Updated) Binary to Decimal
Table of Contents
Hello, today I made a coversion-program that converts binary to decimal here are tips to achieve a program like me the brilliant young koder T :3
Tip 1: Def function #
Instead of having to write the program multiple times we can use a function called defor known as define, its a function that creates function heres an example on what it should look like.
def kodert():
Tip 2: range(start, stop, step) #
We use the range function that returns a sequel of numbers we used this function because when we convert binary to decimal we mostly work from the back and using the help of range and len we can loop it backwords
Tip 3: Calling your function #
Its easy all you got to do is type your function name and a parenteses.
Here is what it should look like
def bin_to_deci(number):
last = len(number) -1
exp = 0
total = 0
for digit in range(last, -1, -1):
# print(number[digit], digit)
total += (int(number[digit])*(2**exp))
exp += 1
#print(total)
return total
if __name__ == '__main__':
answer = bin_to_deci('101010101011100010101010101001010110100110101010')
print("------------------------------------\n")
print(f'Answer is {answer}')
print("\n------------------------------------")
print("")
And here is what the sample output should look like:
------------------------------------
Answer is 187710113671594
------------------------------------
Updated Version #
Hello today I realized when I checked my code because I was curious what would happen if I typed something other than 0 and 1 normally you would think there will be an error so your code should be fine if you thought that, your wrong but if you didnt good job because I found out that the program will just give you the wrong number and it will keep going, so I made an updated version on the binary to decimal conversion program!!!
The things I updated and why #
Here are some of the things I updated instead of a lonely for-loop I gave its best mate a if-else and I added another return function. I updated the for-loop with a if-else because my intention when the user types a invalid input, is for the if-else to check if the number is in 0 or 1 and if it is it will run the rest of the code otherwise it will print a error message along with a return that returns with a value of None but there is another option you can choose you can make the total to None and use the break syntax + I also changed the variable in the for-loop because it was kinda confusing and that is all for today.
what your code should now look like with a better and nicer updated version:3
def bin_to_deci(number):
last = len(number) -1
exp = 0
total = 0
for i in range(last, -1, -1):
#print(f'total: {total}, i: {i}')
if number[i] in '01':
total += int(number[i]) * (2**exp)
exp += 1
else:
print(f"\nError: Binary number contains invalid '{number[i]}'.")
#total = None
#break
return None
return total
if __name__ == '__main__':
answer = bin_to_deci('1021')
print("------------------------------------\n")
print(f'Answer is {answer}')
print("\n------------------------------------")
print("")
Here’s what the sample output should look like for the Error:
Error: Binary number contains invalid '2'.
------------------------------------
Answer is None
------------------------------------
Here’s what the sample output for the non Error:
------------------------------------
Answer is 9
------------------------------------
Bye Koder fans btw Im feeling SUPERCALIFRAGILISTICEXPIALIDOCIOUS :33333333333333