简体   繁体   English

将 Python 函数脚本导入另一个 Python 文件?

[英]Importing Pythons Functions Script into another Python File?

I have this python assignment where I need to do the following: You will create a file myFunctions.py, where you will define/write the functions below:我有这个 python 任务,我需要在其中执行以下操作:您将创建一个文件 myFunctions.py,您将在其中定义/编写以下函数:

min(list) # returns min of a list max(list) # returns max of a list avg(list) # returns average of a list sum(list) # returns sum of a list abs(num) # returns absolute value of num find(key,list)# returns true if key value is found in a list isEven(number) # returns true/false if number is even You will import myFunctions.py into a file testFunctions.py, where you will call/test each of the above functions. min(list) # 返回列表的最小值 max(list) # 返回列表的最大值 avg(list) # 返回列表的平均值 sum(list) # 返回列表的总和 abs(num) # 返回 num 的绝对值find(key,list)# 如果在列表中找到键值,则返回 true isEven(number) # 如果数字为偶数,则返回 true/false 您将 myFunctions.py 导入到文件 testFunctions.py 中,您将在其中调用/测试每个以上功能。

So far, here's what I've got: myFunctions.py:到目前为止,这是我所拥有的:myFunctions.py:

from random import randint

x = randint(5,10)
aList = []
for i in range(0, x, 1):
    number = randint(1,7)
    aList.append(number)
    print(number, end=', ')

def min(aList):
    return min(aList)

def max(aList):
    return max(aList)

sum = 0
 def avg(aList):
sum = 0
for i in range(0,len(aList)):
    sum += aList
    return sum / len(aList)
def sum(aList):
    while True:
        return sum(aList)
        break
def abs(aList):
    return abs(aList[1])

def find(aList):
    x = aList.find()
    return x

def isEven(n):
    if n % 2 == 0:
        return True
    else:
        return False

testFunctions.py: testFunctions.py:

import myFunctions
from myFunctions import max

from random import randint

x = randint(5,10)
aList = []
for i in range(0, x, 1):
    number = randint(1,7)
    aList.append(number)
    print(number, end=', ')

myFunctions.max(aList)

I'm stuck on the parameters of each function and how to use them in the other python file.Any tips?我被困在每个函数的参数上以及如何在另一个 python 文件中使用它们。有什么提示吗?

I'm not entirely sure what you are asking, but if you want to access aList from testFunctions , you can do so as myFunctions.aList .我不完全确定你在问什么,但如果你想从testFunctions访问aList ,你可以这样做myFunctions.aList

However, I think you are confusing the aList in the function definition ( def max(aList):... ) with the aList in testFunctions .不过,我想你混淆了aList在函数的定义( def max(aList):... )与aListtestFunctions You named them the same, but they are different (but related) things.您将它们命名为相同,但它们是不同(但相关)的事物。

When you call myFunctions.max(aList) in testFunctions it is taking aList that you made directly above it in the same file and passing it to the function max .当您在testFunctions调用myFunctions.max(aList) ,它会获取您在同一文件中直接在其上方创建的aList并将其传递给函数max Within max whatever list is the input is given a local variable name aList regardless of the name of the variable when you called max(aList) .max无论您调用max(aList)时的变量名称是什么,输入都会被赋予一个局部变量名称aList If you made a bList in testFunctions the behavior of the max function is the same because aList within the max function refers to whatever list max was given, not a specific list called aList .如果你犯了一个bListtestFunctions的行为max的功能是一样的,因为aList的内max功能是指给予任何列表最大,不叫具体名单aList

It would probably help to rename one of your aList s to something different to avoid confusion.将您的aList之一重命名为不同的名称以避免混淆可能会有所帮助。

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

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