Binary - denary converter problems...

A forum for general discussion of the Python programming language.

Binary - denary converter problems...

Postby pimaster » Wed Sep 25, 2013 6:11 pm

Hi, please help me with my decimal to denary converter. The problem is with the sum total counter 'denarynum', it only seems to add the last number.

Code: Select all
string=str(int(input("Enter a binary number <10000 ")))
stringlength=len(string)
length=int(stringlength)
denarynum=int(0)
if length == 1:
    num1=int(string[0])
    if num1 == 1:
        denarynum=denarynum+1
if length == 2:
    num2=int(string[1])
    if num2 == 1:
        denarynum=denarynum+2
if length == 3:
    num3=int(string[2])
    if num3 == 1:
        denarynum=denarynum+4
if length == 4:
    num4=int(string[3])
    if num4 == 1:
        denarynum=denarynum+8
if length == 5:
    num5=int(string[4])
    if num5 == 1:
        denarynum=denarynum+16
if length == 6:
    num6=int(string[5])
    if num6 == 1:
        denarynum=denarynum+32
if length == 7:
    num7=int(string[6])
    if num7 == 1:
        denarynum=denarynum+64
print("The binary number",string,"in decimal is",denarynum,"in decimal")


Thanks for any help, it is much appreciated! :D
Last edited by micseydel on Wed Sep 25, 2013 7:07 pm, edited 1 time in total.
Reason: Code tags, first post lock.
pimaster
 
Posts: 3
Joined: Tue Sep 24, 2013 4:17 pm

Re: Binary - denary converter problems...

Postby ochichinyezaboombwa » Wed Sep 25, 2013 6:57 pm

Read this.
Also, learn to use loops.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to General Discussions

Who is online

Users browsing this forum: Bing [Bot], Google Feedfetcher and 2 guests