Hi,
Not sure if this is necessary, as I sent another email to the list (I
think), but since I'm new to this thing, I thought I'd do the safe thing and
try a second time.

The code almost works now. Although, as soon as the converter gets a number
-8 or lower, it adds a 0 that is not necessary. 

CODE:
def conversion(n):
    b = ''
    while n > 0:
        r = n%2
        n = n/2
        r = str(r)      
        b = r+b         
    return b    

def signed_mag():
    print ""
    print "--------------------"
    print "Signed Binary"
    print "--------------------"
    print ""
    n = int(raw_input("Please enter a signed integer: "))
    bits = int(raw_input("Please enter the number of bits: "))
    count = conversion(n).count("0") + conversion(n).count("1") \
            #count the amount of digits to know how many 0s to add
    append_zeros = bits - count
    if bits - 1 < count:
        print "Out of range."
    else:
        if n < 0:
            n = abs(n)
            if append_zeros > 0:
                print -n," is encoded as","1"+(append_zeros-4)*"0" \
                +conversion(n),"in Signed Magnitude."
        elif n > 0:
            if append_zeros > 0:
                print n,"is encoded as","0" + (append_zeros-1)*"0" \
                +conversion(n),"in Signed Magnitude."
        else:
            print "That is not an valid signed interger."

I know the problem is at the '(append_zeros-4)' bit, but I'm not sure what
to change/add. Thanks in advance.

Cheers,
Devon

On Wed, 17 Oct 2007 17:02:56 -0400 [EMAIL PROTECTED] wrote:
> You can only count one at a time.
> 
> count = conversion(n).count("0") + conversion(n).count("1")
> 
> count is a string method, so it operates directly on the string - you
> don't have to call it like you did.
> 
> import string
> string.count(mystr, "cheese")
> 
> is the same as
> 
> mystr.count("cheese")
> 
> At least it is in newer versions of python.
> 
> Let me know if that helped.
> 
> Cheers,
> Ben
> 
> On 10/17/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> > I am having trouble getting the string.count function to work. I
> want it to
> > count the amount of digits (0 or 1) in the string, but I keep getting an
> > error stating the string.count was expecting a character buffer object.
> > CODE:
> > count = string.count(conversion(n),["0","1"])
> >
> > ERROR:
> > Traceback (most recent call last):
> >   File "/Users/<name>/Desktop/Project 1.py", line 59, in -toplevel-
> >     signed_mag()
> >   File "/Users/<name>/Desktop/Project 1.py", line 29, in signed_mag
> >     count = string.count(conversion(n),["0","1"])
> >   File
> >
> "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/strin
> g.py"
> > , line 348, in count
> >     return s.count(*args)
> > TypeError: expected a character buffer object
> >
> > I'm trying to make a decimal to binary converter that has the option to
> > select the amount of bits for Signed Binary. I've thought of a way (not
> > tested yet) on how to implement the bits, but I first need to count the
> > amount of digits in the original conversion. Thanks in advance.
> > _______________________________________________
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> 
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to