Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的程序没有给出正确的输出?_Python_Function_If Statement_Absolute - Fatal编程技术网

Python 我的程序没有给出正确的输出?

Python 我的程序没有给出正确的输出?,python,function,if-statement,absolute,Python,Function,If Statement,Absolute,我试图在python上显示-2的绝对值 def distance_from_zero(a): if type(a) == int or type(a) == float: return abs(a) else: return "Nope" distance_from_zero(-2) 节目只说“没有”。我希望它给我-2的绝对值,或者说“nope”是数字不是i

我试图在python上显示-2的绝对值

   def distance_from_zero(a):

            if type(a) == int or type(a) == float:

                 return abs(a)

            else:

                 return "Nope"

   distance_from_zero(-2)

节目只说“没有”。我希望它给我-2的绝对值,或者说“nope”是数字不是int或float。

Add
print
查看输出

def distance_from_zero(a):
    if type(a) == int or type(a) == float:
        return abs(a)
    else:
        return "Nope"

print distance_from_zero(-2)
print distance_from_zero('hi')
输出:

➜  python ./distance.py
2
Nope 

添加
打印
以查看输出

def distance_from_zero(a):
    if type(a) == int or type(a) == float:
        return abs(a)
    else:
        return "Nope"

print distance_from_zero(-2)
print distance_from_zero('hi')
输出:

➜  python ./distance.py
2
Nope 
稍微好一点

def distance_from_zero(dist):
    a = 'Nope'
    if isinstance(dist, int) or isinstance(dist, float):
        a = abs(dist)
    return a
稍微好一点

def distance_from_zero(dist):
    a = 'Nope'
    if isinstance(dist, int) or isinstance(dist, float):
        a = abs(dist)
    return a

请不要将代码作为图像发布。将代码复制粘贴到此问题,并适当设置其格式。请解释“没有给出正确的输出”的确切含义。您是否收到错误消息?请尝试打印结果:
print(distance\u from\u zero(-2))
请不要将代码作为图像发布。将代码复制粘贴到此问题,并适当设置其格式。请解释“没有给出正确的输出”的确切含义。您是否收到错误消息?请尝试打印结果:
print(distance\u from\u zero(-2))
谢谢这给了我绝对值,但如果类型不是int或float,我还需要它返回“Nope”?谢谢!这是我需要的解决方案。谢谢,这给了我绝对值,但如果类型不是int或float,我还需要它返回“Nope”?谢谢!这就是我需要的解决方案。