why can't my tuple be accessed in an inner while loop?

This is the place for queries that don't fit in any of the other categories.

why can't my tuple be accessed in an inner while loop?

Postby pythonnoob123 » Mon Jul 08, 2013 11:37 am

Code: Select all
def func(a,tup2):
   
   k=0
   x=0
   while k<len(a):
      while x<len(tup2):
         if tup2[x]<a[k]:
            a=tuple([a[0:k]])+tuple([tup2[x]])+a[k:]
         if tup2[x]>a[k] and tup2[x]<a[k+1]:
            a=tuple([a[0:k+1]])+tuple([tup2[x]])+a[k+1:]
         x+=1
      k+=1
   return a


here is a function that I am writing that takes in 2 tuples as the argument. When it reaches the line if tup2[x]<a[k]:, it says TypeError: unorderable types: int() < tuple()
I then did some assert statements, with argument a=(1,23,24)

Code: Select all
def func(a,tup2):
   assert a==(1,23,24)
   k=0
   x=0
   while k<len(a):
                assert a==(1,23,24)
      while x<len(tup2):
                        assert a==(1,23,24)
         if tup2[x]<a[k]:
            a=tuple([a[0:k]])+tuple([tup2[x]])+a[k:]
         if tup2[x]>a[k] and tup2[x]<a[k+1]:
            a=tuple([a[0:k+1]])+tuple([tup2[x]])+a[k+1:]
          x+=1
      k+=1
    return a

The compiler receives an error at the 3rd assertion, meaning that for whatever reason, tuple a lost its value at the inner while loop. What happened? I can't seem to find the cause. And when I did similar assert statements to tup2 and put it in place of the third assertion for tuple a, tup2 retained its value. Any help here? Thanks! (Just focus on this problem, and not on any others unrelated to this that might exist in my code).
Last edited by Yoriz on Mon Jul 08, 2013 12:06 pm, edited 1 time in total.
Reason: Added code tags
pythonnoob123
 
Posts: 4
Joined: Mon Jul 08, 2013 11:29 am

Re: why can't my tuple be accessed in an inner while loop?

Postby stranac » Mon Jul 08, 2013 11:58 am

Please read the sticky post titled "New Users, Read This" and edit your post.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1149
Joined: Thu Feb 07, 2013 3:42 pm

Re: why can't my tuple be accessed in an inner while loop?

Postby ochichinyezaboombwa » Mon Jul 08, 2013 6:41 pm

You are changing the value of a in the loop: why it should keep be ==(1,23,24) ?
To debug your code, try to replace assert to
Code: Select all
print(a)


PS: and yes,
stranac wrote:Please read the sticky post titled "New Users, Read This" and edit your post.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 7 guests