Printing


#1

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)
  else:
    return "Nope"
    
distance_from_zero(-2)

#2

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)


#3

Thank you for explaining to me!


#4

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.


#5

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