def Binary_to_Decimal(): Again = "Y" while Again in "Yy": binaryNumber = str(input("Enter a number in binary, to get it converted into decimal: ")) binaryNumber = binaryNumber[::-1] Multiplyer = 1 DecimalNumber = 0 for i in range(len(binaryNumber)): if i == 1: DecimalNumber = DecimalNumber + Multiplyer Multiplyer = Multiplyer * 2 elif i == 0: Multiplyer = Multiplyer * 2 print("That number is decimal is: " + str(DecimalNumber)) print("\n") Again = input("Do you want to convert anymore? (Y or N)") Binary_to_Decimal()

# Binary to Decimal Converter Not Working

Page 1 of 1## 7 Replies - 8028 Views - Last Post: 11 December 2012 - 03:46 PM

### #1

# Binary to Decimal Converter Not Working

Posted 06 December 2012 - 11:05 PM

I have did this out on paper over and over, and the algorithm I have developed seems like it should work. Im not sure why its not working, any help would be appreciated.

##
**Replies To:** Binary to Decimal Converter Not Working

### #2

## Re: Binary to Decimal Converter Not Working

Posted 06 December 2012 - 11:35 PM

It would help if you explained how exactly the code is not working, but at a first glance, your loop does nothing when i is neither 0 nor 1.

This post has been edited by **sepp2k**: 06 December 2012 - 11:35 PM

### #3

## Re: Binary to Decimal Converter Not Working

Posted 06 December 2012 - 11:42 PM

I know it assumes you enter a number in binary.

### #4

## Re: Binary to Decimal Converter Not Working

Posted 07 December 2012 - 01:21 AM

A binary number can still be more than 2 digits long.

### #5

## Re: Binary to Decimal Converter Not Working

Posted 07 December 2012 - 02:28 AM

Okay, so the problem is in your for loop. You're only doing things if i is either 1 or 0, and you're not even doing the right things in those cases. So here's what should actually happen in your for loop:

Here's how to do that in code:

- Pull out the binary digit from the string.
- Multiply it with the multiplier and add it to the DecimalNumber.
- Double the multiplier.
- Proceed with the next digit.

Here's how to do that in code:

def Binary_to_Decimal(): Again = "Y" while Again in "Yy": binaryNumber = str(input("Enter a number in binary, to get it converted into decimal: ")) binaryNumber = binaryNumber[::-1] Multiplyer = 1 DecimalNumber = 0 for i in range(len(binaryNumber)): DecimalNumber += int(binaryNumber[i]) * Multiplyer # Step 1 and 2 Multiplyer *= 2 # Step 3 print("That number is decimal is: " + str(DecimalNumber)) print("\n") Again = input("Do you want to convert anymore? (Y or N)") Binary_to_Decimal()

### #6

## Re: Binary to Decimal Converter Not Working

Posted 07 December 2012 - 08:18 AM

Look at the value in binaryNumber, not the number of i. You can actually do that with:

Also, there is a fundamental logic error: shift then add. Reversing is wrong with that approach. Walk it:

for i in binaryNumber: # do something if i == '1': # do something

Also, there is a fundamental logic error: shift then add. Reversing is wrong with that approach. Walk it:

s = '1011' n = 0 n = n * 2 + 1 = 1 n = n * 2 + 0 = 2 n = n * 2 + 1 = 5 n = n * 2 + 1 = 11

### #7

## Re: Binary to Decimal Converter Not Working

Posted 10 December 2012 - 01:00 PM

Thanks for all your help.

### #8

## Re: Binary to Decimal Converter Not Working

Posted 11 December 2012 - 03:46 PM

I got my entire program working now!

""" Gives the user multiple choices on converting between different number systems used in computer science/programming """ def Decimal_To_Binary_Converter(): """ Takes a decimal base number and converts it to a binary base number """ Again = "Y" while Again in "Yy": DecimalNumber = int(input("Enter a number in decimal, to get it converted into binary: ")) BinaryVersion = int(bin(DecimalNumber)[2:]) print("That decimal number in binary is: " + str(BinaryVersion)) print("\n") Again = input("Do you want to convert anymore? (Y or N) ") print("\n") def Binary_to_Decimal_Converter(): """ Takes a binary base number and converts it to a decimal base number """ Again = "Y" while Again in "Yy": binaryNumber = str(input("Enter a number in binary, to get it converted into decimal: ")) binaryNumber = binaryNumber[::-1] Multiplyer = 1 DecimalNumber = 0 for i in range(len(binaryNumber)): DecimalNumber += int(binaryNumber[i]) * Multiplyer Multiplyer = Multiplyer * 2 print("That binary number in decimal is: " + str(DecimalNumber)) print("\n") Again = input("Do you want to convert anymore? (Y or N) ") print("\n") def Decimal_to_Hex_Converter(): """ Converts a decimal base number to a hexadecimal base number """ Again = "Y" while Again in "Yy": DecimalNumber = int(input("Enter a number in decimal, to have it converted into hexadecimal: ")) HexNum = hex(DecimalNumber) RealHexNum = HexNum[2:] print("That decimal number in hexadecimal is: " + str(RealHexNum)) print("\n") Again = input("Do you want to convert anymore? (Y or N) ") print("\n") def Binary_to_Hex_Converter(): """ Converts a binary base number to a hexadecimal base number """ Again = "Y" while Again in "Yy": BinaryNumber = str(input("Enter a number in binary, to have it converted to hexadecimal: ")) BinaryNumber = BinaryNumber[::-1] Multiplyer = 1 DecimalNumber = 0 for i in range(len(BinaryNumber)): DecimalNumber += int(BinaryNumber[i]) * Multiplyer Multiplyer = Multiplyer * 2 hexVersion = hex(DecimalNumber) hexVersion = hexVersion[2:] print("That binary number in hexadecimal is " + str(hexVersion)) print("\n") Again = input("Do you want to convert anymore? (Y or N) ") print("\n") def Hex_to_Decimal_Converter(): """ Converts a hexadecimal base number to a decimal base number """ Again = "Y" while Again in "Yy": HexValue = input("Enter a number in hexadecimal, to get it converted into decimal: ") DecimalValue = int(HexValue, 16) print("That hexadecimal number in decimal is " + str(DecimalValue)) print("\n") Again = input("Do you want to convert anymore? (Y or N) ") print("\n") def Hex_to_Binary_Converter(): """ Converts a hexadecimal base number to a binary base number """ Again = "Y" while Again in "Yy": HexValue = input("Enter a number in hexadecimal, to get it converted into binary: ") DecimalValue = int(HexValue, 16) BinaryValue = bin(DecimalValue) BinaryValue = BinaryValue[2:] print("That hexadecimal number in binary is " + str(BinaryValue)) print("\n") Again = input("Do you want to convert anymore? (Y or N) ") print("\n") def main(): """ Asks the user which type of conversion they want done, then takes their choice and calls on the appropriate function. Prevents the user from picking an option that is not a choice with an else statement. """ print("Welcome to my Binary and Decimal Converter!") print("------------------------------------------------") print("Press 1 for decimal to binary ") print("Press 2 for binary to decimal") print("Press 3 for decimal to hexadecimal") print("Press 4 for binary to hexadecimal") print("Press 5 for hexadecimal to decimal") print("Press 6 for hexadecimal to binary") print("\n") choice = input("What do you want to do? ") if choice == "1": print("\n") Decimal_To_Binary_Converter() elif choice == "2": print("\n") Binary_to_Decimal_Converter() elif choice == "3": print("\n") Decimal_to_Hex_Converter() elif choice == "4": print("\n") Binary_to_Hex_Converter() elif choice == "5": print("\n") Hex_to_Decimal_Converter() elif choice == "6": print("\n") Hex_to_Binary_Converter() else: print("\n") print("That was not an option, try again!") print("\n") main() main()

Page 1 of 1