Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

decimal->binary python script... yes, I suck at programmi

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
5 posts • Page 1 of 1
Author
Message
Monkeywrench
Apprentice
Apprentice
User avatar
Posts: 205
Joined: Tue Jun 22, 2004 3:50 am
Location: Florida

decimal->binary python script... yes, I suck at programmi

  • Quote

Post by Monkeywrench » Fri Sep 24, 2004 12:07 am

sorry if this is not the forum for these sorts of questions..
Python's my first language, I started learning it before the summer of this year, but never really got into it. Anyway, I'm trying to pick it up.. I wrote a silly little program to convert decimal values to binary. I'd just like suggestions on what could be done in a better, more efficient way. Thanks

Code: Select all

#!/usr/bin/python

def convert(decvalue):
        """This will convert the decimal input value to binary and return it"""
        print decvalue,"is the decimal value to convert"
        binnum=""
        while decvalue != 0:
                remain=decvalue%2
                decvalue=decvalue/2
                if remain == 1:
                        binnum = "1" + binnum
                else:
                        binnum = "0" + binnum
        return binnum    

deci=input("Give me the decimal value: ")
print convert(deci)
Sample output:

Code: Select all

Give me the decimal value: 21
21 is the decimal value to convert
10101
And another thing.. before, I had the line binnum="" outside of the function (in other words, I put the line right after the deci=input... line) but I got an error:

Code: Select all

UnboundLocalError: local variable 'binnum' referenced before assignment
Erm, what's this mean? :oops:
Folding@home -Join the Linux team (163)!
Top
_dook_master_
Apprentice
Apprentice
Posts: 295
Joined: Tue Sep 16, 2003 2:14 am
Location: Isla Vista, CA

Re: decimal->binary python script... yes, I suck at progr

  • Quote

Post by _dook_master_ » Fri Sep 24, 2004 12:41 am

Monkeywrench wrote:sorry if this is not the forum for these sorts of questions..
Python's my first language, I started learning it before the summer of this year, but never really got into it. Anyway, I'm trying to pick it up.. I wrote a silly little program to convert decimal values to binary. I'd just like suggestions on what could be done in a better, more efficient way. Thanks

Code: Select all

#!/usr/bin/python

def convert(decvalue):
        """This will convert the decimal input value to binary and return it"""
        print decvalue,"is the decimal value to convert"
        binnum=""
        while decvalue != 0:
                remain=decvalue%2
                decvalue=decvalue/2
                if remain == 1:
                        binnum = "1" + binnum
                else:
                        binnum = "0" + binnum
        return binnum    

deci=input("Give me the decimal value: ")
print convert(deci)
Sample output:

Code: Select all

Give me the decimal value: 21
21 is the decimal value to convert
10101
And another thing.. before, I had the line binnum="" outside of the function (in other words, I put the line right after the deci=input... line) but I got an error:

Code: Select all

UnboundLocalError: local variable 'binnum' referenced before assignment
Erm, what's this mean? :oops:
I'm not sure how Python handles things, as I don't write in it, but from common logic I'd assume that since you label binnum="", that it thinks it is a string.
Top
SZwarts
l33t
l33t
User avatar
Posts: 629
Joined: Mon Oct 13, 2003 10:08 pm
Location: Sydney, NSW, Australia
Contact:
Contact SZwarts
Website

  • Quote

Post by SZwarts » Fri Sep 24, 2004 12:48 am

  • * Your "if" statement is redundant
    * You can use operator= assignemnts like += /= that doesn't make anything more or less efficient, just easier to read
    * You can do without the variable remain

Code: Select all

UnboundLocalError: local variable 'binnum' referenced before assignment
Erm, what's this mean?
It means that you're using a read out before an assigment. It's in the line
binnum= "1"+binnum. Since you don't declare type in python explicitely python has no idea what binnum is. So it first tries to calculate "1"+binnum and then tries to assign it to binnum. But because the type of binnum is unknown it does not no if it can actually "add" binnum to the string "1" (maybe it's not a string but an integer and how can you add an integer to a string?), hence you're not allowed to use variables in python before you have assigned them.
So see the binum="" as a type declaration :)
Well hope that was clear enough...

Code: Select all

def convert(decvalue):
  print decvalue,"is the decimal value to convert"
  binnum=""
  while decvalue !=0:
    binnum=str(decvalue%2)+binnum
    decvalue/=2
  return binnum

deci=input("Give me the decimal value: ")
print convert(deci)
only when it is dark enough, can you see the stars
Top
Monkeywrench
Apprentice
Apprentice
User avatar
Posts: 205
Joined: Tue Jun 22, 2004 3:50 am
Location: Florida

  • Quote

Post by Monkeywrench » Sat Sep 25, 2004 4:09 am

Thanks for the input :)

SZ: wow.. your version is quite a bit shorter.. heh :oops: could you explain this line: "decvalue/=2" does it just mean "set decvalue equal to decvalue/2"?

Thanks again
Folding@home -Join the Linux team (163)!
Top
SZwarts
l33t
l33t
User avatar
Posts: 629
Joined: Mon Oct 13, 2003 10:08 pm
Location: Sydney, NSW, Australia
Contact:
Contact SZwarts
Website

  • Quote

Post by SZwarts » Mon Sep 27, 2004 12:51 am

Monkeywrench wrote: SZ: wow.. your version is quite a bit shorter.. heh :oops: could you explain this line: "decvalue/=2" does it just mean "set decvalue equal to decvalue/2"?
Yes.

Example:

Code: Select all

i=0
i+=1 #i=1
i+=3 #i=4
i*=3 #i=12
i/=4 #i=3

teststring="test"
teststring+="ing" #teststring="testing"
only when it is dark enough, can you see the stars
Top
Post Reply

5 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy