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

  • Home
  • SEARCH
  • 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 7903177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:40:56+00:00 2026-06-03T09:40:56+00:00

I am a newbie on python. I have just create a program taking a

  • 0

I am a newbie on python. I have just create a program taking a 2’s complement binary number and convert it to decimal value. (The way of conversion is described at http://sandbox.mc.edu/~bennet/cs110/tc/tctod.html.)

I am aware that there are some certain rules on how to format your program and some “good habits” on how to design your program. Like how you put a header, comment, etc. And how you design the structure of it. I could not find a guide on Internet so I decide to ask here.

This is my first time making post on stackoverflow, so please bear with me if I make any mistakes. : )

Here is my code.

def secBiToDecimal(number):
""" This program takes a 2's complement binary number as input and returns its decimal value
"""


output = ""
" Check the sign of this number and calculate its value in according way."

if number[0]=='0':
    output += "+"
    temp = 0
    for i in range(1,len(number)):
        temp += (int(number[i]) * (2**(len(number)-i-1)))
    output += str(temp)
    print output
elif number[0]=='1':
    output += "-"
    carryout = 1
    " flip the digits"
    number = list(number)
    for i in range(len(number)):
        if number[i] == "1":
            number[i]='0'
        else:
            number[i]='1'

    " add 1 to number in binary sense "

    for i in range(1,len(number)):
        if carryout == 0 and number[len(number)-i]=='0':
            break
        elif carryout == 1 and number[len(number)-i]=='0':
            number[len(number)-i]='1'
            break
        elif carryout == 0 and number[len(number)-i]=='1':
            number[len(number)-i]='1'
            break
        elif carryout == 1 and number[len(number)-i]=='1':
            number[len(number)-i]='0'
    number = "".join(number)
    temp = 0
    for i in range(1,len(number)):
        temp += int(number[i]) * (2**(len(number)-1-i))
    output += str(temp)
    print output
  • 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-06-03T09:40:58+00:00Added an answer on June 3, 2026 at 9:40 am

    One of the most important things to consider with regard to code formatting, styling and conventions is the “house rules”. If your “house” (i.e. work place, team, school, teacher, etc.) expects things done in a certain way, then that is the way you should do it.

    Standards and conventions found on the internet can be used as source material for discussions on changing the house rules, or to develop your own personal standard for personal projects.

    But do use some standard, and keep an offline copy of the documentation for that standard if you can, so that you can read your own code 6, 12, 24 months down the line ;).

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

Sidebar

Related Questions

I am a newbie in Python. I have written following program and expecting all
I'm newbie in Python. I have this simple code a = 0 b =
Python newbie here and wanted to know if there was a way to combine
I am a Python newbie. I have this small problem. I want to print
I'm a python newbie with a problem too hard to tackle. I have a
Apologies, somewhat confused Python newbie question. Let's say I have a module called animals.py
I'm a Python & App Engine (and server-side!) newbie, and I'm trying to create
I'm a newbie to python and the app engine. I have this code that
I am a very newbie in Python I have the following code: from SOAPpy
I'm a Python newbie. I have been a little confused by the differences between

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.