如何导入我在python中创建的函数?

2024-05-20 09:38:32 发布

您现在位置:Python中文网/ 问答频道 /正文

抱歉问这么简单的事。。。你知道吗

我制作了这个factorial.py文件:

# Find the factorial of a number
def fact(n):
    p = 1
    for i in range(1, n+1):
        p = p*i
    return p
if __name__ == '__main__':
    n = int(input('Enter an integer to find the factorial of: '))
    f = fact(n)
    print('Factorial of {0}: {1}'.format(n, f))

保存在C:\Users\usersname

然后在新的idle我写了from factorial import fact, 但它导致了一个错误,如下图所示:

enter image description here


Tags: 文件ofthenameinpynumberfor