导入模块并执行导入的函数

问题描述 投票:0回答:1

我创建了一个程序,其中包含一个主要函数和两个自定义函数来计算球体的半径。现在我需要将该程序作为一个模块导入到一个新程序中,该程序会提示用户提出相同的问题,然后显示相同的结果。我坚持的部分是如何从我的原始程序执行导入的功能。我将包括我的第一个程序“program5_2”的代码,然后是下一个“program5_3”,这是我需要执行导入函数的代码。什么都会帮助谢谢!

import shere

def main():
    sphere_radius = float(input('Enter radius of sphere '))
    area = sphere.surface_area(spehere_radius)
    print(f'The surface area of a sphere with radius {sphere_radius} is {area:.4f}')
    sphere_volume = sphere.volume(sphere_radius)
    print(f'The volume is {sphere_volume:.3f}')
if __name__ == '__main__':
    main()
import program5_2

def main():
    sphere_radius = float(input('Enter radius of sphere '))

    print(f'The surface area of a sphere with radius {sphere_radius} is {area:.4f}')
    print(f'The volume is {sphere_volume:.3f}')
if __name__ == '__main__':
    main()
python function module python-import python-module
1个回答
0
投票

如果我理解正确,你想从

sphere.surface_area()
调用
program5_3
函数,但你想使用
sphere
program5_2
导入而不是再次导入它。

为此,您可以将

sphere
模块引用为
program5_2.sphere
或将其导入为
from program5_2 import sphere
.

但我建议将

sphere
直接导入
program5_2
中。

© www.soinside.com 2019 - 2024. All rights reserved.