#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    1
    Rep 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. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Try something like this:
    Code:
    arraylen = len(value)
    element = 1
    while element <= arraylen:
         print value[-element],
         element = element+1
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    Last edited by netytan; November 21st, 2003 at 07:40 AM.
    programming language development: www.netytan.com Hula

  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Netytan,
    That reverse slice is a trick.
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I know, extended slices rock so much!!! very useful, especially with list compressions

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


IMN logo majestic logo threadwatch logo seochat tools logo