What does this error mean? I'm trying to make a function that returns a tuple. I'm sure i'm doing all wrong. Any help is appreciated.
from random import randint
A = randint(1,3)
B = randint(1,3)
def make_them_different(a,b):while a == b:a = randint(1,3)b = randint(1,3)return (a,b)
new_A, new_B = make_them_different(A,B)
Your code returns None
if a != b
.
Since, you have the return
statement inside the while loop, if the while loop never gets executed, Python returns the default value of None
which cannot be assigned to new_A, new_B
.
>>> print make_them_different(2, 3)
None>>> print make_them_different(2, 2)
(2, 1)
You could fix this by returning the default values (since they are different and that's what you intend to do)
def make_them_different(a,b):while a == b:a = randint(1,3)b = randint(1,3)return (a,b) # Dedented the return line.
Demo -
>>> make_them_different(2, 2)
(3, 2)
>>> make_them_different(2, 3)
(2, 3)