[Tutor] Binary Real to Decimal

Chris Castillo ctcast at gmail.com
Mon Mar 30 05:42:43 CEST 2009


myinput = raw_input("Please enter a binary real number:  ")
myinput = myinput.split(".")

binstr1 = myinput[0]
binstr2 = myinput[1]

decnum1 = 0
decnum2 = 0

for i in binstr1:
    decnum1 = decnum1 * 2 + int(i)

for k in binstr2:
    decnum2 = decnum2 * 2 + int(k)



print "\nThe binary real number ", binstr1, ".", binstr2, " converts to ",
decnum1,".",decnum2," in decimal."


that is what I have so far but I need to create a condition where I need
only 10 sufficient numbers from the variable decnum2. I know I need
something like
if len(decnum2) > 11:
    decnum2 = decnum2[0:11]

but I keep getting unsubscriptable errors. I know it has to do with types
but it's late and I just need some help. thank you in advance. - chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090329/5e8dad78/attachment.htm>


More information about the Tutor mailing list