简体   繁体   English

使用 function 内的用户输入或计算值

[英]Use user input or calculated value within function

Say I define a function:假设我定义了一个 function:

def calculate_bmi(height):
    bmi = weight / (height)**2
    print('BMI:',bmi)

I have saved this as functions.py in the same folder as the file for the code where I call this function.我已将其作为functions.py保存在与我称之为 function 的代码文件相同的文件夹中。 I am using jupyter notebook for the main code.我正在使用 jupyter notebook 作为主要代码。

The variable weight is calculated in the main code, and not known to the user calling the function.变量weight主代码中计算,调用 function 的用户不知道。 Hence, it cannot be given as an input parameter.因此,它不能作为输入参数给出。

So, when the function is called in the main code:因此,当 function 在主代码中被调用时:

import functions
from functions import calculate_bmi

weight = calculate_weight() # weight is calculated 
calculate_bmi(2)

I get the following error:我收到以下错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-c66651ccce8b> in <module>
      2 from functions import bmi
      3 weight = calculate_weight() # weight is calculated 
----> 4 calculate_bmi(2)

D:\PP\functions.py in calculate_bmi(height)
     75 
     76 def calculate_bmi(height):
---> 77     bmi = weight / (height)**2
     78     print('BMI:',bmi)

NameError: name 'weight' is not defined

Is there a way to make python use the values already calculated earlier in the code, within the function as they are already stored in it's memory?有没有办法让 python 使用代码前面已经计算的值,在 function 中,因为它们已经存储在它的 memory 中?

You need to pass weight as a variable to your function, ie您需要将权重作为变量传递给您的 function,即

weight = calculate_weight()  
## weight = input('Input weight:') - for user input

def calculate_bmi(height, weight):
    bmi = weight / (height)**2  
    return bmi

bmi = calculate_bmi(5, weight)
print('BMI:',bmi)

Also, name your functions with respect to what they do, not the variables they produce.此外,根据它们的作用命名您的函数,而不是它们产生的变量。

In Python, an imported function can't access the variables of the module it's called in. Its access ends at the module where it's defined .在 Python 中,导入的 function 无法访问它所调用的模块的变量。它的访问在它定义的模块处结束。 You might have been confused by the term "global scope", since it's not really "global", it's module scope.您可能对“全局范围”一词感到困惑,因为它并不是真正的“全局”,它是模块scope。

The best way to solve this problem is to make weight a parameter, as @crusher083 recommended in their answer , but FYI, it's possible to edit variables in the global scope of an imported module.解决此问题的最佳方法是将weight作为参数,正如@crusher083 在他们的答案中推荐的那样,但仅供参考,可以在导入模块的全局 scope 中编辑变量。 For example:例如:

import functions

functions.weight = 215
functions.calculate_bmi(2)  # -> BMI: 53.75

This is probably a bad idea in this case, but this technique is useful for monkey-patching modules that you can't edit yourself.在这种情况下,这可能是个坏主意,但这种技术对于您无法自己编辑的猴子补丁模块很有用。

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

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