python - Function that returns a tuple gives TypeError: 'NoneType' object is not iterable -
what error mean? i'm trying make function returns tuple. i'm sure i'm doing wrong. appreciated.
from random import randint = randint(1,3) b = randint(1,3) def make_them_different(a,b): while == b: = 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, have return
statement inside while loop, if while loop never gets executed, python returns default value of none
cannot assigned new_a, new_b
.
>>> print make_them_different(2, 3) none >>> print make_them_different(2, 2) (2, 1)
you fix returning default values (since different , that's intend do)
def make_them_different(a,b): while == b: = randint(1,3) b = randint(1,3) return (a,b) # dedented return line.
demo -
>>> make_them_different(2, 2) (3, 2) >>> make_them_different(2, 3) (2, 3)
Comments
Post a Comment