patternpythonMinor
Binary to decimal number converter
Viewed 0 times
numberdecimalbinaryconverter
Problem
Here I am doing some exercises for my studies with Python. This is a binary to decimal number converter (currently restricted to that).
These are the very first steps, so there is no error handling. You can even input non-binary numbers and get a result. However it works for converting binary numbers, which is its task.
I'm not sure about all shenanigans going around with the main function and the argument parsing.
(Update: Added parentheses to
These are the very first steps, so there is no error handling. You can even input non-binary numbers and get a result. However it works for converting binary numbers, which is its task.
I'm not sure about all shenanigans going around with the main function and the argument parsing.
import argparse
import sys
###############################################################################
def main(binary_number):
binary_number = list(binary_number)
decimal_number = 0
for digit in range(0, len(binary_number)):
if binary_number[digit] != '0':
decimal_number += power_to_the_people(2, len(binary_number)-digit-1)
print(decimal_number)
###############################################################################
def power_to_the_people(base, exponent):
# Return 1 if exponent is 0
if exponent == 0:
return 1
power = base
for j in range(1, exponent):
power *= base
return power # to the people
###############################################################################
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Converts a binary to a decimal number.')
parser.add_argument('binary_number', action="store")
args = parser.parse_args()
main(args.binary_number)(Update: Added parentheses to
print so it actually works under 3.4)Solution
A few additional points for your learning experience.
You're not using
In
so instead of
Don't use pointless comments like this:
Instead if this:
It would be better this way:
Because in this version
and
It would be better to rename it to
And while at it,
it would be better to move the content from the
Many people miss this point, actually.
The reason to do it this way is that code in the
So variables with the same name in the rest of the code will shadow those variables,
which can lead to nasty bugs.
By moving that code to a method,
this cannot happen.
PEP8
PEP8 is the official style guide of Python,
and you have a few violations:
-
Put 2 blank lines before each method definition and you only put 1.
-
Put 2 spaces in front of
-
Lines should not be longer than 79 characters
You're not using
sys, so the import line is unnecessary.In
range(...), the default start is 0,so instead of
range(0, x) you can write simply range(x).Don't use pointless comments like this:
# Return 1 if exponent is 0
if exponent == 0:
return 1Instead if this:
power = base
for j in range(1, exponent):
power *= baseIt would be better this way:
power = 1
for j in range(exponent):
power *= baseBecause in this version
base is referenced only once,and
range is simplified by dropping 1 to use the default 0.action="store" is unnecessary here:parser.add_argument('binary_number', action="store")main is not a good name for that function does.It would be better to rename it to
binary_to_decimal, for example.And while at it,
it would be better to move the content from the
if __name__ ... inside a new main method, like this:def main():
parser = argparse.ArgumentParser(description='Converts a binary to a decimal number.')
parser.add_argument('binary_number')
args = parser.parse_args()
binary_to_decimal(args.binary_number)
if __name__ == '__main__':
main()Many people miss this point, actually.
The reason to do it this way is that code in the
if __name__ ... block is in the global namespace.So variables with the same name in the rest of the code will shadow those variables,
which can lead to nasty bugs.
By moving that code to a method,
this cannot happen.
PEP8
PEP8 is the official style guide of Python,
and you have a few violations:
-
Put 2 blank lines before each method definition and you only put 1.
-
Put 2 spaces in front of
# inline comments-
Lines should not be longer than 79 characters
Code Snippets
# Return 1 if exponent is 0
if exponent == 0:
return 1power = base
for j in range(1, exponent):
power *= basepower = 1
for j in range(exponent):
power *= baseparser.add_argument('binary_number', action="store")def main():
parser = argparse.ArgumentParser(description='Converts a binary to a decimal number.')
parser.add_argument('binary_number')
args = parser.parse_args()
binary_to_decimal(args.binary_number)
if __name__ == '__main__':
main()Context
StackExchange Code Review Q#69573, answer score: 4
Revisions (0)
No revisions yet.