Jump to content

A bit of fun for the board


Sparda

Recommended Posts

Um, if you dont type anything, it ends, if not, it loops through putting each character of your input into an array, counting backwards, but then at the end returns nothing? I have no clue, I'm not much of a programmer, and dont use Python.

Link to comment
Share on other sites

convert 'input' to 'output' which is a string representation of 'input' in binary?

That's the ticket ;) Couldn't find a python function or pre existing code for this. You may wonder why this is necessary, but the answer lies in when doing things with values that are stored in a byte but you only need part of that byte.

>>> valtobin.valtobin(1)
'1'
>>> valtobin.valtobin(100)
'1100100'
>>> valtobin.valtobin(255)
'11111111'
>>> valtobin.valtobin(int('f0',16))
'11110000'

Link to comment
Share on other sites

So it snto actually the binary representtion of the characters, say, text to ascii, then ascii to binary, but the number of characters length written in binary? Like if I wrote "hello" it would return "000000101"? Was your code intended to count numbers to binary? What happens when you input text?

http://www.daniweb.com/code/snippet216539.html

Link to comment
Share on other sites

So it snto actually the binary representtion of the characters, say, text to ascii, then ascii to binary, but the number of characters length written in binary? Like if I wrote "hello" it would return "000000101"?

It takes a positive integer and produces it's binary representation.

Link to comment
Share on other sites

so 5 would return 101 then...but what happens when someone types in letter? Will it crash?

Link to comment
Share on other sites

I find this works better (just ported it from pascal->python using your first few lines - I assume you had a reason for input=int(str(input)) ):

def num2bin( input ):
    input = int( str( input ) )

    if input==0:
        return 0

    copyinput = input

    output=""

    while( copyinput > 0 ):
        output="%s%s" % ( str( copyinput%2 ), output )
        copyinput-=copyinput%2
        copyinput/=2

    return output

Link to comment
Share on other sites

Awesome, I am in the process of learning python right now.

humm my guess would be..

you set it at the beginning so it does not cause a blank input error by code

"if input == 0:

return 0"

im not familuar with commands such as -= and *= yet only the basics like ==, <=, !=, ect..

i need to read more, im about to learn more about the elif syntax, my guess is the amazingfunction outputs an infinite listofpossibles? also..

"listofpossibles = []

while copyinput > 0:

if copyinput - x >= 0:

copyinput -= x

listofpossibles.insert(0, x)"

I am guessing "listofpossibles = []" the "[]" part is an array? and "listofpossibles.insert(0,x)" after the dot the ".insert(0,x)" is getting the input and inserting it for the array of listofpossibles? im probuly way off

Link to comment
Share on other sites

Psychosis, thanks for your help man.. after reading what you wrote I found this ..

http://www.tutorialspoint.com/python/pytho...c_operators.htm

ELIF statement..

http://www.tutorialspoint.com/python/python_if_else.htm

Python looks very promising, I hope the above helps someone else also, I will be back here again to check the links out :)

I will have to look more into python arrays later after I get the loops, syntax, variables, math and such first..

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...