#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Help in a simple program


    hello!
    I've just started to learn programming, and the first language I learn is Python.
    My mission was to write a program that convert a decimal number to binary.
    I wrote it like this:
    def binary (n):
    if n==0 or n==1:
    print n
    else:
    if n%2==0:
    return str(binary(n/2))+"0"
    else: return str(binary(n/2))+"1"

    and the result this:
    ================================
    >>>
    >>> binary(20)
    1
    'None0100'
    >>>
    >>> binary(899)
    1
    'None110000011'

    the output is correct, but how can I change the program that it wil be without the word "none" in the middle of the number?

    thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    When n is 0 or 1, your function doesn't return anything, which in Python is equivalent to returning None. str(None) is 'None', which is why you're seeing it in your output.

    Next time, be sure to put code tags [code][/code] around your code. Python code listings are particularly useless without them.

IMN logo majestic logo threadwatch logo seochat tools logo