Jan-27-2020, 05:42 AM
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.
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
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


