PDA

View Full Version : Simple Python Assignment (That I Cannot figure out)




iHerzeleid
Feb 3, 2008, 10:22 PM
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.



swiftaw
Feb 3, 2008, 10:25 PM
Didn't you post about this already?

(Quick Search)

Oh yeah, here: http://forums.macrumors.com/showthread.php?t=425960

iHerzeleid
Feb 3, 2008, 10:28 PM
yea i did. but i asked the mods to delete that one. its under the wrong section

eddietr
Feb 3, 2008, 10:57 PM
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
Feb 4, 2008, 12:09 AM
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[i]

eddietr
Feb 4, 2008, 12:11 AM
reversing the loop. using the "rules" mentioned.

OK, so you have the list from the user.

Do you understand how to iterate through the list using a for loop?

iHerzeleid
Feb 4, 2008, 12:39 AM
no, not really. and right now i feel like a fool. i know its going to be something easy.

eddietr
Feb 4, 2008, 12:46 AM
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[i]


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
Feb 4, 2008, 01:02 AM
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
Feb 4, 2008, 01:09 AM
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
Feb 4, 2008, 01:33 AM
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
Feb 4, 2008, 01:45 AM
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
Feb 4, 2008, 02:04 AM
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
Feb 4, 2008, 10:41 AM
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
Feb 4, 2008, 05:26 PM
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
Feb 4, 2008, 05:35 PM
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
Feb 4, 2008, 05:42 PM
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
Feb 4, 2008, 05:58 PM
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
Feb 4, 2008, 06:35 PM
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
Feb 4, 2008, 07:23 PM
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.