Hi
I have got the TypeError. I use lists at the same way in my other programs it works excellent, but just in this program I have got problem don’t know how to solve it?
Traceback (most recent call last):
File "E:\extra_b.py", line 42, in <module>
ordna(pile)
File "E:\extra_b.py", line 22, in ordna
new_num = num-1
TypeError: unsupported operand type(s) for -: 'list' and 'int'

import random
def lasin ():
     c=int(input ("Enter an integer "))
     return c                     
def dela(c):
     sum=0
     pile = []
     while sum < c:
         b = random.randrange (1, 10 - sum + 1)
         pile.append(b)
         sum += 1
     print pile,sum
     return sum, pile
def ordna(x):
    b = 0
    y = []
    for num in x:
        new_num = num-1
        if new_num<0:
            del new_num
        else:
            y.append(new_num)
            b+=1
    y.append (b)
    y.sort()
    y.reverse ()
    print " ".join(map(str,y))
    return y
c=lasin ()
pile=dela(c)
ordna(pile)

Thanks a lot for your help.

Recommended Answers

All 7 Replies

Instead of debugging, if it's OK, I'll talk about *how* to debug.

Since the error is coming from line 42 (line 31 in the code above), insert a print statement just prior to line 31:

...
pile = dela(c)
print pile
ordna(pile)

That way, you can check your assumption that "pile" is a list of numbers. (Hint: it isn't!)

Jeff

I see I want to work with a list of number and my first function return a list of number. I don’t want to mix a deferent type int and list.
But when I did as you said, I got a deferent list which looks like that:
First print --> [2, 5, 7, 7] 4
Second print --> (4, [2, 5, 7, 7])
I don’t know why they are deferent?
May be same where my program has mixed a deferent kind of type?? I don’t see it !

Obviously this is not a list of numbers, so you want to take a look at the function that returns this. Add print statements to see what is going on throughout the program until you understand what is happening

First print --> [2, 5, 7, 7] 4
Second print --> (4, [2, 5, 7, 7])

the first print is showing 2 different objects.
The second print is showing 1 object.

Read about lists & tuples, embedding in particular, this may help you understand

Chris

Hi
Thanks guys I find the reason of error but I still don’t understand fallowing.
My first function dela ( ) return pile and sum, pile is a list of numbers and sum is a digit but I used only pile which is a list and sent it to my other function. Sum which is a digit and returns by function but didn´t used.
If a function return a, b can I use only a?? And how b can influence result of a??

I have changed my cod and it works now.

def dela(c):
     sum=0
     pile = []
     while sum < c:
         b = random.randrange (1, 10 - sum + 1)
         pile.append(b)
         sum += 1
     return pile
def dela(c):
     sum=0
     pile = []
     while sum < c:
         b = random.randrange (1, 10 - sum + 1)
         pile.append(b)
         sum += 1
     print pile,sum
     return sum, pile
def ordna(x):
    b = 0
    y = []
    for num in x:
        new_num = num-1
        if new_num<0:
            del new_num
        else:
            y.append(new_num)
            b+=1
    y.append (b)
    y.sort()
    y.reverse ()
    print " ".join(map(str,y))
    return y
c=lasin ()
pile=dela(c)
ordna(pile)

This is fine you can use both a and b that are returned, when you return them in the way you have the result is a tuple.

just means that you have to access each bit indervidual.

so to use the list of numbers that is returned in this case you would have to put

ordna(pile[1])

If you print them you will see that you can access both

print pile[0]
print pile[1]

Notice that the first line will print the interger that you returned and the second line will print the list of numbers you returned.

Chris

To work the way you want, your original code would have been
c=lasin ()
sum, pile=dela(c)
ordna(pile)
If the function returns two variables, but the calling line only receives one variable, then python returns one tuple, because you only have one block of memory allocated for the return.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.