TypeError: cannot unpack non-iterable NoneType object when running a sorting function

Max1234

I'm trying to create a simple sorting function for 3 inputs, and I've managed to do so when all inputs are unique.

However, I'm encountering issues when 2 of the 3 inputs are the same, more specifically, when the 2nd and 3rd input are the same. I get the following issue: TypeError: cannot unpack non-iterable NoneType object

What does this mean and how can it be fixed?

def simple_sort_version2(a,b,c):
    if a > b and a > c:
        if b > c:
            return c,b,a
        elif c > b:
            return b,c,a
    elif b > a and b > c:
        if a > c:
            return c,a,b
        elif c > a:
            return a,c,b
    elif c > a and c > b:
        if a > b:
            return b,a,c
        elif b > a:
            return a,b,c
    elif a == b:
        if a > c:
            return c,a,b
        elif c > a:
            return a,b,c
    elif a == c:
        if a > b:
            return b,a,c
        elif b > a:
            return a,c,b
    elif b == c:
        if b > a:
            return a,b,c
        elif a > b:
            return b,c,a

a,b,c = simple_sort_version2(10,20,30)
print (a,b,c) # 10 20 30

a,b,c = simple_sort_version2(10,30,20)
print (a,b,c) # 10 20 30

a,b,c = simple_sort_version2(30,20,10)
print (a,b,c) # 10 20 30

a,b,c = simple_sort_version2(30,20,20)
print (a,b,c) # 20 20 30
Aven Desta
>>>print(simple_sort_version2(30,20,20))
   None

since a>b and a>c your code enters the first if-statement Then it returns None because there is no b==c case.

Note that:

If there is no return statement python returns None by default

The code can not go to the last elif-statement because it already entered the first if-statement

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

This error occurs when I'm running my code on terminal "TypeError: cannot unpack non-iterable NoneType object"

Error occurs running code in terminal "TypeError: cannot unpack non-iterable NoneType object"

TypeError: cannot unpack non-iterable NoneType object

-- TypeError: cannot unpack non-iterable NoneType object

PyAutoGui TypeError: cannot unpack non-iterable NoneType object

TypeError: cannot unpack non-iterable NoneType object Error

"TypeError: cannot unpack non-iterable int object" with filter function

Calling local variable causes TypeError: cannot unpack non-iterable NoneType object

I am getting: TypeError: cannot unpack non-iterable NoneType object, but I am returning a tuple

Python 3.8.2| Why do I have a : TypeError: cannot unpack non-iterable NoneType object?

Python error- cannot unpack non-iterable NoneType object

TypeError: cannot unpack non-iterable int object in Django views function

Why am I getting TypeError: cannot unpack non-iterable builtin_function_or_method object?

Find smaller number from 2 provided - works, but throws an error. TypeError: cannot unpack non-iterable NoneType object

TypeError: cannot unpack non-iterable GetColorImage object

tensorflow TypeError: cannot unpack non-iterable float object

TypeError: cannot unpack non-iterable int object

Python Django: TypeError: cannot unpack non-iterable MatchAll object

TypeError at /cart/ cannot unpack non-iterable Cart object

TypeError: cannot unpack non-iterable bool object

Python: TypeError: cannot unpack non-iterable int object

Python TypeError: cannot unpack non-iterable bool object

Python / TypeError: cannot unpack non-iterable Jugador object

TypeError: cannot unpack non-iterable int object (Python)

TypeError: cannot unpack non-iterable Lexer object

How to fix TypeError: cannot unpack non-iterable Button object

Cannot unpack non-iterable object

(TypeError: cannot unpack non-iterable int object) for inversion counter using merge sort

I am getting a "TypeError: cannot unpack non-iterable int object"