SunQuest
           Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Try It Free
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old November 20th, 2003, 09:46 PM
Mock26 Mock26 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 1 Mock26 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 09:50 PM.

Reply With Quote
  #2  
Old November 21st, 2003, 04:28 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
Try something like this:
Code:
arraylen = len(value)
element = 1
while element <= arraylen:
     print value[-element],
     element = element+1

Reply With Quote
  #3  
Old November 21st, 2003, 06:24 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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 06:40 AM.

Reply With Quote
  #4  
Old November 21st, 2003, 04:31 PM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
Netytan,
That reverse slice is a trick.

Reply With Quote
  #5  
Old November 21st, 2003, 04:37 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
I know, extended slices rock so much!!! very useful, especially with list compressions

Mark.

Reply With Quote
Reply

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


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway