Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7535525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:19:29+00:00 2026-05-30T06:19:29+00:00

so i have started the code, and i know that everything after the ‘else’

  • 0

so i have started the code, and i know that everything after the ‘else’ is probably wrong:

def binary(n):
    if n < 2:
        print (n)
    else:
        x = (bin(int(n)//2)
        print (x)

it should do this recursively:

>>> binary(0)
0
>>> binary(1)
1
>>> binary(3)
11
>>> binary(9)
1001

i do need the function to print the binary representation rather than return.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:19:30+00:00Added an answer on May 30, 2026 at 6:19 am

    This will not work as expected, as you have print in two places, you will end up with multiple lines, e.g.:

    >>> def binary(n):
    ...   if n < 2:
    ...     print n
    ...   else:
    ...     binary(n / 2)
    ...     print n % 2
    ... 
    >>> binary(0)
    0
    >>> binary(1)
    1
    >>> binary(3)
    1
    1
    >>> binary(9)
    1
    0
    0
    1
    >>> binary(10)
    1
    0
    1
    0
    

    Other answers use strings, so here’s one with lists: 🙂

    >>> def binary(n):
    ...   if n < 2:
    ...     return [n]
    ...   else:
    ...     return binary(n / 2) + [n % 2]
    ... 
    >>> binary(0)
    [0]
    >>> binary(1)
    [1]
    >>> binary(3)
    [1, 1]
    >>> binary(9)
    [1, 0, 0, 1]
    >>> binary(10)
    [1, 0, 1, 0]
    

    and if you really want a string, it’s as simple as this: 🙂

    >>> ''.join(map(str, binary(10)))
    '1010'
    

    Of course, since you’ve already found out about the function bin, you should probably have done this in the first place:

    >>> bin(10)[2:]
    '1010'
    

    How this reminds me of this:

    • http://www.ariel.com.au/jokes/The_Evolution_of_a_Programmer.html

    Happy coding! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started working on some code left behind by previous developers, and I'm
We have started using the boost unit testing library for a large existing code
My company recently purchased TFS and I have started looking into the code analysis
I'm an avid vim user and have started to write some SQL code recently.
Question simple and quick: I have started to use Netbeans to write some code
We have recently started using FxCop on our code base and I am in
I have just started using jQuery and although following code gets the job done,
I have recently started using DocCheck for checking the validity of JavaDoc's in code
I have just installed C# 2008 Express Edition 2008 and started writing code. My
I just started using php arrays (and php in general) I have a code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.