Python Forum
HELP! Return Statement Debugging
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HELP! Return Statement Debugging
#1
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
Reply
#2
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()
Reply
#3
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(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()
Reply
#5
@michael1789, what's the purpose of the while True? Why put everything in a infinite loop? and line #6 - empty return?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with Return statement Columbo 13 2,179 Sep-17-2022, 04:03 PM
Last Post: Columbo
  How to invoke a function with return statement in list comprehension? maiya 4 2,752 Jul-17-2021, 04:30 PM
Last Post: maiya
  syntax error on return statement l_butler 5 3,027 May-31-2020, 02:26 PM
Last Post: pyzyx3qwerty
  return statement will not work TheTechRobo 2 2,588 Mar-30-2020, 06:22 PM
Last Post: TheTechRobo
  Embedding return in a print statement Tapster 3 2,233 Oct-07-2019, 03:10 PM
Last Post: Tapster
  return statement usage SB_J 3 2,389 Jul-16-2019, 07:24 PM
Last Post: snippsat
  I don't understand this return statement 357mag 4 2,704 Jul-10-2019, 07:02 PM
Last Post: perfringo
  Return Statement in Python IDLE editor NMW 10 10,455 Jul-11-2017, 09:47 PM
Last Post: NMW
  Need help understanding return statement python_lover 4 4,324 Mar-03-2017, 10:09 AM
Last Post: python_lover
  Python basics about __init__ and return statement wk14 5 5,924 Oct-25-2016, 04:31 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020