Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iHerzeleid

macrumors 6502a
Original poster
May 5, 2007
555
0
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.
 

iHerzeleid

macrumors 6502a
Original poster
May 5, 2007
555
0
yea i did. but i asked the mods to delete that one. its under the wrong section
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
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?
 

iHerzeleid

macrumors 6502a
Original poster
May 5, 2007
555
0
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
 

iHerzeleid

macrumors 6502a
Original poster
May 5, 2007
555
0
no, not really. and right now i feel like a fool. i know its going to be something easy.
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
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



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.
 

iHerzeleid

macrumors 6502a
Original poster
May 5, 2007
555
0
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()
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
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()

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.
 

iHerzeleid

macrumors 6502a
Original poster
May 5, 2007
555
0
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.
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
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.

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.
 

iHerzeleid

macrumors 6502a
Original poster
May 5, 2007
555
0
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!
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
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!

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.
 

SC68Cal

macrumors 68000
Feb 23, 2006
1,642
0
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.
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
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.

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.
 

SC68Cal

macrumors 68000
Feb 23, 2006
1,642
0
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?
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
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.

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.

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

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.
 

SC68Cal

macrumors 68000
Feb 23, 2006
1,642
0
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.

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.
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.