Below is a code I tried, but for some reason, it does not print anything out

def distance_from_zero(potato):
  if type(potato) == int or type(potato) == float:
    return abs(potato)
    print distance_from_zero(potato)
    return "Nope"


by default, a function returns None at the end of the function

if we want to return something else at the end of the function, we can use the return keyword. Thus, when a return keyword is reached, the function ends

to print what the function returns, we can include a print statement before the function call

also, be glad return abs(potato) ends the function, the line below would otherwise have caused endless recursion (sort of like an infinity loop, but then with functions)


Thank you for explaining to me!


you should also remove the function call which is in the function body, this is known as recursion (a function calling itself) and without a base case, its very similair to a infinity loop.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.