Sparda Posted January 23, 2010 Share Posted January 23, 2010 Can you tell what it is yet (don't run it, that's cheating): hint: it's in python Quote Link to comment Share on other sites More sharing options...
digip Posted January 23, 2010 Share Posted January 23, 2010 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. Quote Link to comment Share on other sites More sharing options...
Netshroud Posted January 23, 2010 Share Posted January 23, 2010 convert 'input' to 'output' which is a string representation of 'input' in binary? Quote Link to comment Share on other sites More sharing options...
Sparda Posted January 23, 2010 Author Share Posted January 23, 2010 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' Quote Link to comment Share on other sites More sharing options...
digip Posted January 23, 2010 Share Posted January 23, 2010 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 Quote Link to comment Share on other sites More sharing options...
Sparda Posted January 23, 2010 Author Share Posted January 23, 2010 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. Quote Link to comment Share on other sites More sharing options...
digip Posted January 23, 2010 Share Posted January 23, 2010 so 5 would return 101 then...but what happens when someone types in letter? Will it crash? Quote Link to comment Share on other sites More sharing options...
Sparda Posted January 23, 2010 Author Share Posted January 23, 2010 so 5 would return 101 then...but what happens when someone types in letter? Will it crash? Indeed, it can only take an integer. The input = int(str(input)) is rather superfluous I suppose. Quote Link to comment Share on other sites More sharing options...
Netshroud Posted January 23, 2010 Share Posted January 23, 2010 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 Quote Link to comment Share on other sites More sharing options...
555 Posted January 23, 2010 Share Posted January 23, 2010 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 Quote Link to comment Share on other sites More sharing options...
Netshroud Posted January 23, 2010 Share Posted January 23, 2010 a*=b is a=a*b a+=b is a=a+b a-=b is a=a-b a/=b is a=a/b It's shorthand. listofpossibles=[] is IIRC a Tuple. I've never heard of them before Python, but they seem like the equivalent of VB.NET (and probably other .NET)'s ArrayList. Quote Link to comment Share on other sites More sharing options...
555 Posted January 24, 2010 Share Posted January 24, 2010 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.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.