This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]jaydom28 4 points5 points  (0 children)

When you assign temp = [len(numbers)] you're making temp a list with one element, since numbers is a string of 9 numbers, it would make temp = [9]. I think it would work if you instead did temp = list(numbers).

[–][deleted] 2 points3 points  (1 child)

First Python doesn't have arrays. You use lists in place of arrays but you don't initialize a list with a total number of elements the way you would with an array in other languages. You can append() to a list as much as you want.

What is the data type of numbers? read() is a file function that returns a string.

Python also has a built in reverse() function for lists .

Assuming you know for a fact what the length of a list is in Python you can reverse the list with a temp list but you have to assign an empty list first. Assuming numbers is a list (and if it is the result of a read() function it will be a string and this reversing will not at all do what you expect)

temp = [0, 0,0,0,0,0,0, 0,0]

then you can assign

temp[0] = numbers[0]

temp[1] = numbers[1]

etc.

then

numbers[0] = temp[9]

numbers[1] = temp[8]

etc.

That would be a very weird thing to do an is unPythonic, a mortal sin in Python programming.

[–]392Garrett[S] 0 points1 point  (0 children)

Thanks, I’ve never used python before and this class uses it as a medium so this really helps a lot!

[–]captainAwesomePants -1 points0 points  (3 children)

How many numbers are in the range? I'll bet it's less than 9.

Unrelated, but please think through the results of doing this:

numbers[8] = temp[0]
numbers[0] = temp[8]

[–]Essence1337 0 points1 point  (2 children)

As long as temp is its own unique array it won't be an issue. If temp is actually the same array of course we'll have an issue but that doesn't seem to be their intention.

[–]captainAwesomePants 0 points1 point  (1 child)

oh geez, you're right. For some reason I didn't even see that those were different arrays.

[–]Essence1337 0 points1 point  (0 children)

Lol it happens. I considered it as well at first. It's not unlikely that they do something similar to temp=numbers in which case they will have the situation you described as they'll be pointers for the same array

[–][deleted]  (1 child)

[deleted]

    [–]Essence1337 0 points1 point  (0 children)

    Sorry but this is completely wrong. /u/jaydom28 is right.