简体   繁体   English

如何在python中的函数中使用函数

[英]How to use a function within a function in python

I want to use the function f in the function radiationExposure , but this code return nothing. 我想在函数radiationExposure使用函数f ,但是此代码未返回任何内容。

def f(x):
    import math
    return 10*math.e**(math.log(0.5)/5.27*x)


def radiationExposure(start, stop, step):    
    if start < 0 or stop < start or step < 0:
        print("Invalid inputs!")
    else:
        result = 0 # Total radiation exposure area
        for i in range(start, stop + 1):
            result += f(i)*step
        return result


radiationExposure(5, 10, 1)

When you run a Python script, it will not auto-echo the result. 当您运行Python脚本时,它不会自动回显结果。 Your code runs fine, but you need to explicitly print the result: 您的代码运行良好,但是您需要显式打印结果:

print(radiationExposure(5, 10, 1))

Your script, when run, will now print 22.9424104106 . 您的脚本在运行时现在将打印22.9424104106

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM