November 20th, 2003, 09:46 PM
Must crawl before I can walk. Help with a basic python program!
I am taking an intro class into programming, and I need some help. I have to create an array that is x-number of cells long (I'm using 10 for now because it is small. I then have to create a program that will take a base ten number and convert it into binary using the "divide by 2 and take the remainder" method. This is not a problem. My problem is that I then have to print the array backwards, starting with the last number that was put into the array. I can't figure it out! Here is my main program and some of the various print options (I have been using the command "print value" merely to test that the binary conversion was working fine. Can anyone help me on this? Thanks!
PS I know this is so incredibly simple, but it is my first time programming anything so please bear with me.
PPS My indents did not carry over into the forum posting, sorry.
n=input("enter a number to convert to binary")
while i > 0 and a>=0:
# print counter
# while i>=counter:
# counter = counter -1
# print "The number ",n, " in binary is ",[i]
Last edited by Mock26; November 20th, 2003 at 09:50 PM.
November 21st, 2003, 04:28 AM
Try something like this:
arraylen = len(value)
element = 1
while element <= arraylen:
element = element+1
November 21st, 2003, 06:24 AM
another way to do this using the power of Pythons for loop and the reverse() method..
of course this will activly reverse the whole array which can be a porblem if you want to use it for something else after that, so you'll have to call reverce again which sucks! So i think this would be much better (needs Python 2.3)
for element in array:
for element in array[::-1]:
Last edited by netytan; November 21st, 2003 at 06:40 AM.
November 21st, 2003, 04:31 PM
That reverse slice is a trick.
November 21st, 2003, 04:37 PM
I know, extended slices rock so much!!! very useful, especially with list compressions