Python Forum
HELP! Return Statement Debugging - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: HELP! Return Statement Debugging (/thread-24004.html)



HELP! Return Statement Debugging - HappyMan - Jan-27-2020

Dear All
I am a beginner to Python. I wrote the code below but does not understand why when I key in "0" and then a negative number (says "-5"), the print line appeared twice -- and I have no idea how the second print line (ie. "The Absolute Value of 0 is None" come into the picture. Please help.

def absoluteValue(n):
    if n < 0:
        return -n
    elif n > 0:
        return
    else:
        print("Please key in a value greater or lower than 0 !!\t ")
        main()

def main():
    y = input("Key in an integer : \t")
    n = int(y)
    print("The Absolute Value of", y, " is ", absoluteValue(n))

main()
Result :
Key in an integer : 0
Please key in a value greater or lower than 0 !!
Key in an integer : -5
The Absolute Value of -5 is 5
The Absolute Value of 0 is None

LOL LOL LOL


RE: HELP! Return Statement Debugging - michael1789 - Jan-27-2020

To be sure you know the absolute value of n can be found with abs(n).

And the absolute value of 0 is None. It has no value. You can add as many as you want and still 0. Not a math person exactly, but I think absolute value is the measurement of a number from 0 in integers. 5 and -5 are both absolute 5 as they are both 5 whole numbers from 0.

(Jan-27-2020, 05:51 AM)michael1789 Wrote: the print line appeared twice -- and I have no idea how the second print line

Oh, I missed this. You'll note that your second print line is actually the output for the first entry. You are calling main() from with another function, it runs that execution of main() then finishes the first one it started when it drops back out.

This works:
while True:
    def absoluteValue(n):
        if n < 0:
            return -n
        elif n > 0:
            return
        else:
            print("Please key in a value greater or lower than 0 !! ")
            
     
    def main():
        y = input("Key in an integer : ")
        n = int(y)
        print("The Absolute Value of", y, " is ", absoluteValue(n))
     
    main()



RE: HELP! Return Statement Debugging - perfringo - Jan-27-2020

Maybe this assignment is not wholly disclosed (why should there be message that num must be greater or lower than zero?).

If you need to write a function which returns absolute value of number then it must work correctly. Absolute value of 0 is 0 (zero distance from zero is zero). It can be easily 'verified' by using Python's built-in abs() function:

>>> abs(0)
0
This essentially means that in order to get absolute value one must change only negative values. Therefore function can be written:

def my_abs(num):
    if num < 0:              # if number is negative, change to positive
        return -num
    else:
        return num           # if number is 0 or positive keep as it is
Regarding printing None if zero value is entered: in case of zero function prints message and don't return anything. All functions which don't return (or yield) anything return None. So message is printed and None is returned by function.


RE: HELP! Return Statement Debugging - michael1789 - Jan-27-2020

(Jan-27-2020, 08:19 AM)perfringo Wrote: All functions which don't return (or yield) anything return None.

Sorry, my bad. Got it now. You aren't returning n. The function just ends, making it "fruitless".

while True:
    def absoluteValue(n):
        if n < 0:
            return -n
        elif n > 0:
            return
        else:
            print("Please key in a value greater or lower than 0 !! ")
            return n      """<------ Fixes it"""
     
    def main():
        y = input("Key in an integer : ")
        n = int(y)
        print("The Absolute Value of", y, " is ", absoluteValue(n))
     
    main()



RE: HELP! Return Statement Debugging - buran - Jan-27-2020

@michael1789, what's the purpose of the while True? Why put everything in a infinite loop? and line #6 - empty return?


RE: HELP! Return Statement Debugging - michael1789 - Jan-27-2020

The infinite part is bad form. Line 6 was already like that, I missed it.

Sorry for my sloppiness.

Better:
def absoluteValue(n):
    if n < 0:
        return -n
    elif n > 0:
        return n
    else:
        print("Please key in a value greater or lower than 0 !! ")
        return n
 
def main():
    y = input("Key in an integer : ")
    n = int(y)
    print("The Absolute Value of", y, " is ", absoluteValue(n))

run = True
while run:     
   
    main()
    cont = input("Wish to continue? y/n: ")
    if cont == "y":
        run = True
    if cont == "n":
        run = False