Last active
November 8, 2018 14:48
-
-
Save goxr3plus/12242d8950ae887d8a8271e685b5e455 to your computer and use it in GitHub Desktop.
Python Binary to Decimal , Python Decimal to Binary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` PYTHON | |
#----------------Convert Binary to Decimal and check for validity---------------------- | |
def isBinary(s): | |
try: | |
return set(s) <= set('01') | |
except ValueError: | |
return False | |
t = 1 | |
while t > 0: | |
binaryNumber = input("Enter a binary No.") | |
if(isBinary(binaryNumber)): | |
print("It is binary") | |
else: | |
continue | |
convertedNumber = int(binaryNumber, 2) | |
print(convertedNumber) | |
print("") | |
#----------------------Convert Decimal to Binary and check for validity---------------------- | |
def RepresentsInt(s): | |
try: | |
int(s) | |
return True | |
except ValueError: | |
return False | |
t = 1 | |
while t > 0: | |
binaryNumber = input("Enter a decimal No.") | |
if ( RepresentsInt(binaryNumber) ): | |
print("It is Number") | |
else: | |
continue | |
convertedNumber = bin(int(binaryNumber)) | |
print(convertedNumber[2:]) | |
print("") | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment