Python Forum
Why do I get this traceback?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why do I get this traceback?
#1
>>> def orderize(array):
...     is_complete = False
...     i, x = 0
...     ordered_list = []
...     while not is_complete:
...             if array[i] == x:
...                     ordered_list.append(array[i])
...             if ordered_list.__len__() == 5:
...                     is_complete = True; continue
...             if i == 4:
...                     i, x = 0
...                     continue
...             i+=1
...             x+=1
... 
>>> orderize((4,3,2,1,0))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in orderize
TypeError: 'int' object is not iterable
also, why any other optimal solutions for ordering a list? noteworthy: the things getting ordered will be an object with an attribute that has the value of where it needs to be... I just noticed that this would forever loop.
Reply
#2
Should i, x = 0 be i = x = 0? I don't understand your second question.
Reply
#3
(Sep-20-2016, 04:55 AM)micseydel Wrote: Should i, x = 0 be i = x = 0? I don't understand your second question.

oh i think you're correct. I am somewhat new to actually programming in python. I've only played around with it a few times.

I was asking why i got that traceback... so what does i, x mean versus i = x = 0. I haven't coded in a while and knew a bunch of languages. So i guess that is other syntactic sugar in c or something.

>>> orderize((4,3,2,0,1))
[2, 2, 2, 2, 2]
When running this I got this. Which is quite embarassing. I'm going to go rewrite this. If you have any optimal ways of writing this please tell me. I'm looking for something a bit more concise.
Reply
#4
With a comma, Python tries to "unpack" an iterable on the right hand side, and associate the relevant things to the sequences of variables on the left hand side. With equals, it acts as chaining, I believe the same behavior as C.
Reply
#5
(Sep-20-2016, 04:43 AM)Gengar Wrote:
>>> def orderize(array):
...     is_complete = False
...     i, x = 0
...     ordered_list = []
...     while not is_complete:
...             if array[i] == x:
...                     ordered_list.append(array[i])
...             if ordered_list.__len__() == 5:
...                     is_complete = True; continue
...             if i == 4:
...                     i, x = 0
...                     continue
...             i+=1
...             x+=1

Lets start at the top of your while loop and work our way down...
1) why are you comparing the content of the array at a certain index... with that index? (x and i always have the same value, since you always increment them together and set them to 0 together).
2) why are you using __len__() instead of len()?
3) why are you comparing the length of the ordered list to 5?  You'll only ever be sorting arrays that are exactly 5 items long?  Why not... "if len(ordered_list) == len(array):"?
4) "if i==4:"... what?  Why?  Why would you care if the index is 4?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020