why does my function is returning data type None??

2019-10-20 20:23发布

This is my python code for printing an absolute number. My function is returning type None. I am not getting what I have done wrong. Please help me.

def n(num):
    if num<0:
        return (num*-1)

no = input("Enter a number: ")
print "Absolute Value is: "
print n(no)

3条回答
放荡不羁爱自由
2楼-- · 2019-10-20 20:50

You really don't need your own function for absolute value...

no = input("Enter a number: ")
print "Absolute Value is: ", abs(no)
查看更多
Explosion°爆炸
3楼-- · 2019-10-20 20:57

The return is on the condition. Try :

def n(num):
    if num<0:
        num *= -1
    return num

no = int(input("Enter a number: "))
print "Absolute Value is: "
print n(no)
查看更多
【Aperson】
4楼-- · 2019-10-20 21:01
def n(num):
    if num<0:
        return (num*-1)
    else:
        return num


no = input("Enter a number: ")
print "Absolute Value is: "
print n(no)

writing an else statement will return num >= 0

Thank You :)

查看更多
登录 后发表回答