Re: [Tutor] Binary to Decimal conversion
"A.T.Hofkamp" wrote If you reverse the computation, it gets even simpler: binstr = raw_input("Please enter a binary number: ") decnum = 0 for i in binstr: decnum = decnum * 2 + int(i) But if we are allowed to use int() it is easier still: decnum = int(raw_input("Please enter a binary number"), 2) Since int() now converts binary strings to decimal... :-) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Binary to Decimal conversion
Hello, Chris Castillo wrote: Thanks for clearing that up. I knew it was much simpler than I was trying to make it I just couldn't quite see the logic that I needed for the problem clearly. Thanks for the elegant code. On Mon, Mar 9, 2009 at 10:53 PM, Moos Heintzen wrote: binnum = raw_input("Please enter a binary number: ") decnum = 0 rank = 1 for i in reversed(binnum): decnum += rank * int(i) rank *= 2 If you reverse the computation, it gets even simpler: binstr = raw_input("Please enter a binary number: ") decnum = 0 for i in binstr: decnum = decnum * 2 + int(i) print decnum If you want to preserve the binary spirit of the conversion, you should of course do for i in binstr: decnum = (decnum << 1) | int(i) instead. Sincerely, Albert ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Binary to Decimal conversion
Thanks for clearing that up. I knew it was much simpler than I was trying to make it I just couldn't quite see the logic that I needed for the problem clearly. Thanks for the elegant code. On Mon, Mar 9, 2009 at 10:53 PM, Moos Heintzen wrote: > You're making it more complicated than it needs to. > Also, you first used binnum then binum, and you didn't define binsum. > > It could easily be done like this: > > binnum = raw_input("Please enter a binary number: ") > decnum = 0 > rank = 1 > > for i in reversed(binnum): >decnum += rank * int(i) >rank *= 2 > > Moos > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Binary to Decimal conversion
You're making it more complicated than it needs to. Also, you first used binnum then binum, and you didn't define binsum. It could easily be done like this: binnum = raw_input("Please enter a binary number: ") decnum = 0 rank = 1 for i in reversed(binnum): decnum += rank * int(i) rank *= 2 Moos ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Binary to Decimal conversion
Chris Castillo wrote: I am having some difficulties in producing the correct code for a simple binary to decimal conversion program. The arithmetic I want to use is the doubling method - so if I wanted to get the decimal equivalent of 1001, I would start with multiplying 0 * 2 and adding the left most digit. I would then take the sum of 1 and multiply it by 2 and add the next digit to that product and so on and so forth until I come to an end sum of 9. I seem to come up with something like this: binnum = raw_input("Please enter a binary number: ") for i in range(0, len(binum), 1): item = "0" if i < len(binum) - 1: item = binum[i + 1] binsum = binsum * int(item) * 2 + binsum + int(binum[i]) print "\nThe binary number ", binum, " you entered converts to", binsum, " in decimal." Having said that I will point out 1 - to access elements of a sequence you may write: for item in binnum: 2 - the first element of binnum is binnum[0]. Your code starts by accessing the 2nd element. 3- you use binsum before assigning anything to it. That is bound to raise a Name error. 4 - I don't understand binsum = binsum * int(item) * 2 + binsum + int(binum[i]) That does not agree with your verbal algorithm. -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Binary to Decimal conversion
Chris Castillo wrote: I am having some difficulties in producing the correct code for a simple binary to decimal conversion program. The arithmetic I want to use is the doubling method - so if I wanted to get the decimal equivalent of 1001, I would start with multiplying 0 * 2 and adding the left most digit. I would then take the sum of 1 and multiply it by 2 and add the next digit to that product and so on and so forth until I come to an end sum of 9. I seem to come up with something like this: binnum = raw_input("Please enter a binary number: ") for i in range(0, len(binum), 1): item = "0" if i < len(binum) - 1: item = binum[i + 1] binsum = binsum * int(item) * 2 + binsum + int(binum[i]) print "\nThe binary number ", binum, " you entered converts to", binsum, " in decimal." I can't really figure out what is going wrong here. I don't see anything going wrong. All I see is some Python code. Would you be kind enough to tell us why you think something is going wrong. If there is an error post the traceback If you get a result you did not expect tell us what the input was, the desired result and the actual result. -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Binary to Decimal conversion
I am having some difficulties in producing the correct code for a simple binary to decimal conversion program. The arithmetic I want to use is the doubling method - so if I wanted to get the decimal equivalent of 1001, I would start with multiplying 0 * 2 and adding the left most digit. I would then take the sum of 1 and multiply it by 2 and add the next digit to that product and so on and so forth until I come to an end sum of 9. I seem to come up with something like this: binnum = raw_input("Please enter a binary number: ") for i in range(0, len(binum), 1): item = "0" if i < len(binum) - 1: item = binum[i + 1] binsum = binsum * int(item) * 2 + binsum + int(binum[i]) print "\nThe binary number ", binum, " you entered converts to", binsum, " in decimal." I can't really figure out what is going wrong here. Please help me - this has been driving me insane. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor