Basic Python Help

Discussion in 'Web Design and Development' started by iHerzeleid, Feb 1, 2008.

  1. iHerzeleid macrumors 6502a


    May 5, 2007
    im a newb at this and just got into it

    how can i solve this

    Write a program that asks the user to enter a list, and which then outputs
    the reverse of the list. Here is an example:

    Please enter a list: [1,6,11]

    The reverse of [1, 6, 11] is [11, 6, 1]

    any idea. and im not supposed to be using the reverse command
  2. angelwatt Moderator emeritus


    Aug 16, 2005
    First, this sounds like a homework assignment. If it is that's fine, but make sure to state that in your message. We don't like doing people's homework for them, but its fine to give you hints and lead you in the right direction.

    Is this going to be a Python program run from a command line, or is it being developed in a web page? In the example you gave, do the square brackets ([, ]) need to be there? Also what parts of the program do you really need help on; asking for user input, reading the input in, the logic of reversing the numbers, etc.?
  3. elppa macrumors 68040


    Nov 26, 2003
    Well this is what I'd do (may not be the best idea):

    [1] Make a collection which contains the items of the list
    [2] Create a new list.
    [3] Loop through the collection created in step 1 backwards.
    [4] On each iteration add the item to the end of the list created in step 2.
    [5] Spit out the result.
  4. SC68Cal macrumors 68000

    Feb 23, 2006
    Just create a loop that decrements the index and prints the list rather than incrementing the index and printing.

    In other words:
    Since a list can have n elements in the list and is zero-based indexed (starts at zero) start at n -1 (since it's zero-based index) and count down to zero, printing as you go along.
  5. dcr macrumors member

    Jun 10, 2002
    Here's a hint:

    This is two lines in python.

    For the second line, consult the python documentation on list types. Section 3.1.4
  6. SC68Cal macrumors 68000

    Feb 23, 2006

Share This Page