Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 20th, 2003, 10:46 PM
 Mock26
Junior Member

Join Date: Nov 2003
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
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!

ciao,
john.

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.

value=range(10)
i=0
n=input("enter a number to convert to binary")
a=n
b=2
#q=a/b
rem=0
counter=0

while i > 0 and a>=0:
q=a/b
rem=a-b*q
value[i]=rem
i=i+1
a=q
counter=counter+1
# print counter

#else:
# while i>=counter:
# i=i-1
# print[i]
#
# counter = counter -1

#counter=i
#while counter>=0:
# print[i]
# i=i-1
# counter=counter-1

#else:
# print "The number ",n, " in binary is ",[i]
# i=i+1

print value

Last edited by Mock26 : November 20th, 2003 at 10:50 PM.

#2
November 21st, 2003, 05:28 AM
 Grim Archon
Mini me.

Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 13
Try something like this:
Code:
```arraylen = len(value)
element = 1
while element <= arraylen:
print value[-element],
element = element+1```

#3
November 21st, 2003, 07:24 AM
 netytan
Hello World :)

Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
another way to do this using the power of Pythons for loop and the reverse() method..

Code:
```array.reverse()
for element in array:
...
do whatever
...```

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)

Code:
```for element in array[::-1]:
...
do whatever
...```

Mark.
__________________
programming language development: www.netytan.com Hula

Last edited by netytan : November 21st, 2003 at 07:40 AM.

#4
November 21st, 2003, 05:31 PM
 Grim Archon
Mini me.

Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 13
Netytan,
That reverse slice is a trick.

#5
November 21st, 2003, 05:37 PM
 netytan
Hello World :)

Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
I know, extended slices rock so much!!! very useful, especially with list compressions

Mark.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Must crawl before I can walk. Help with a basic python program!