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
>>>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 defaultThe code can not go to the last
elif-statement
because it already entered the firstif-statement
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments