Simple Python Assignment (That I Cannot figure out)

Discussion in 'Mac Programming' started by iHerzeleid, Feb 3, 2008.

  1. macrumors 6502a

    iHerzeleid

    Joined:
    May 5, 2007
    #1
    yes i am a noob. anywayas this is for a class i will be taking next quarter but i am doing it now for various reasons.

    this is it

    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: [4,5,6]
    The reverse of [4,5,6] is [6,5,4]

    Hint: You can assume that the user enters a correct list of integers. Note that such a list starts with
    ‘[’ and ends with ‘]’, i.e., both need to be entered by the user. Use a for loop and the + operation to
    concatenate lists and list elements.


    i am lost so badly at the moment and i really need help. anyone help please?

    thanks. or at least a little budge will help.
     
  2. macrumors 603

    swiftaw

    Joined:
    Jan 31, 2005
    Location:
    Omaha, NE, USA
    #2
  3. thread starter macrumors 6502a

    iHerzeleid

    Joined:
    May 5, 2007
    #3
    yea i did. but i asked the mods to delete that one. its under the wrong section
     
  4. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #4
    OK, so this problem consists of two major parts right:

    One is getting the user's input and making a list out of it.

    And two is reversing the list using a for loop.

    So which part is giving you trouble?
     
  5. thread starter macrumors 6502a

    iHerzeleid

    Joined:
    May 5, 2007
    #5
    reversing the loop. using the "rules" mentioned.

    im sure this isnt right because its not working for me *sigh so frustrating*

    numb = input("Enter the numbers: ")
    x_list = numb
    for i in range(len(x_list)):
    print "L[", i, "] =", _xlist
     
  6. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #6
    OK, so you have the list from the user.

    Do you understand how to iterate through the list using a for loop?
     
  7. thread starter macrumors 6502a

    iHerzeleid

    Joined:
    May 5, 2007
    #7
    no, not really. and right now i feel like a fool. i know its going to be something easy.
     
  8. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #8



    OK, well let me give you a couple of hints that I hope can help.

    First don't think about printing at this point. Leave that for later.

    And you are correctly using input to read and evaluate what the user types in. So if they enter [4,5,6] you'll get a list of [4,5,6].

    Now, just FYI, you don't need the double assignment to numb and then to x_list. You could do x_list = input ("Enter...: ")

    So now you have x_list, the trick is to create a new list, called it y_list. So do that first and then you can print both x_list and y_list like the example output they gave.

    Look at your for loop again. Let's say x_list is [4,5,6]. They len(x_list) is 3. And the range(3) is [0,1,2]. So instead of iterating through [4,5,6], you are actually iterating through [0,1,2].

    Does that make sense?

    Open a python shell and enter the following:

    >>> a = [4,5,6]
    >>> len(a)
    >>> range(len(a))

    and you'll see what I mean.
     
  9. thread starter macrumors 6502a

    iHerzeleid

    Joined:
    May 5, 2007
    #9
    i get stuck with this error

    what does it mean? and thanks so much. i feel like an idiot but when i get it itll be better

    for i in range(len(n)):
    TypeError: object of type 'int' has no len()
     
  10. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #10
    Well, I'm not sure what you're trying to do now. I guess n is an int? In which case there is no len() of an int, which is what it is saying.

    Here's the thing:

    you have a list of [4,5,6] for example.

    In the previous code where you had range(len(x_list)) what you were doing is:

    1.) Computing the length of [4,5,6], which is 3.
    2.) Making a new list of length 3 using the range operator. That new list is [0,1,2]
    3.) This the key: you were then iterating over the new list of [0,1,2]

    So if you think about it, instead of making a new list of [0,1,2] and iterating over that, why not just iterate over the perfectly good list you already had?

    Hint: you're making this harder than it really is.
     
  11. thread starter macrumors 6502a

    iHerzeleid

    Joined:
    May 5, 2007
    #11
    i made this


    n = input("Enter the numbers you wish to list : ")
    x = n
    for i in range(len(n)):
    print i

    for i in range(len(x)):
    print i[::-1]


    i still dont understand. the first loop should list the numbers (but not in a list i dont know how) and the second one doesnt work.
     
  12. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #12
    A couple of hints:

    Forget about the printing for now. The goal here is to have the user enter a list and then have you create a new list with the items in reverse. So focus on that.

    And please go back and read my previous post about what happens when you do:

    range(len(x_list)) or range(len(any_list))

    Did you try entering what I suggested into the python interpreter:

    >>> a = [4,5,6]
    >>> len(a)
    >>> range(len(a))

    If you can do that and understand what you get back from each line, then you'll figure this problem out immediately.

    Otherwise, you'll end up just guessing and changing variable names and ending up with the same problem.

    Try entering those three lines into an interpreter, and see what you get back. And then explain to me in your own words what happens.

    And then explain in your own words what python does with:

    range(len(any_list))

    Then you'll have it, I promise.
     
  13. thread starter macrumors 6502a

    iHerzeleid

    Joined:
    May 5, 2007
    #13
    ok for some reason i assumed that if the user enters the numbers: 1 2 and 3 for ex. it will format it into [1,2,3] but it doesnt do that automatically.

    ok so i wrote this from scratch

    orig_list = raw_input("Enter the numbers you wish to list : ")
    new_list = orig_list
    for i in range(len(new_list)):


    print "The reverse of " ,orig_list, "is" ,new_list,

    now it seems to print out the list that the user inputs. but how do i reverse it. thanks im almost there!
     
  14. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #14
    Right, the user should be typing in "[4,5,6]"

    I think you might be going on the wrong track here, I'm afraid. All you've done is copy one reference to the other with:

    new_list = orig_list

    So that doesn't really get you any closer.

    Here's another hint: There is absolutely no reason to use either range() or len() in this exercise. So throw those out and this will be much easier.
     
  15. macrumors 68000

    Joined:
    Feb 23, 2006
    #15
    First suggestion for making a list out of user input:

    Since you know that your boundaries for your input is going to be a String containing a "[" and a "]" you can discard them using slices. You know where they will be, as long as the user follows the input method of "[a,b,c,...,n]"

    After that, just split the string based on the delimiter, which in this case is a comma.

    From there, it's again like my post in your old thread. Start your loop index at the last element and count down to the first.
     
  16. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #16
    Well, if you are going to assume that the user is typing it as [a,b,c], then you can just use input(), which calls eval().

    There is no need to parse the user's input.

    This whole script is 4-5 lines long, maximum.
     
  17. macrumors 68000

    Joined:
    Feb 23, 2006
    #17
    Yes, input() would work, but it will not teach the concepts that are supposed to be practiced. I assume that since the assignment explicitly states the "[" and "]" I assumed that string manipulation was a goal.

    Also, won't input create a list with one entry that just holds the entire string?
     
  18. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #18
    That may be. I assumed something quite different, which is that the instructor specified that so that it would be easier. In order words the instructor is saying "assume the user enters the data in exactly the format you need for a list."

    In this case, the OP is learning how to iterate through a list, so I think maybe string parsing maybe something he doesn't want to tackle right now. My guess is the instructor was thinking the same thing.

    No, it uses eval. So if a user enters [4,5,6], you will get a list with 3 elements, exactly as you want for the exercise.
     
  19. macrumors 68000

    Joined:
    Feb 23, 2006
    #19
    OK - I was using raw_input() rather than input(). That would definitely be a cause. I'm wary of eval() though because it seems like you're giving out a whole lot of latitude with user input since eval() treats input as a string representing a Python expression.
     
  20. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #20
    Of course, you would want to be careful about using input() in a real world scenario.

    But again, this guy is just trying to learn how to reverse a list using a for loop. Just a basic class exercise.
     

Share This Page