gpt4 book ai didi

python - 如何在我的主脚本中运行另一个脚本

转载 作者:行者123 更新时间:2023-11-30 22:55:26 25 4
gpt4 key购买 nike

我不知道如何在我的主 Python 脚本中运行另一个脚本。例如:

索引.py:

  Category = "math"
Print (Category)
Print ("You can use a calculator")
Print ("What is 43.5 * 7")
#run calculator.py
Answer = int(input("What is your answer"))

如何在其中运行计算器脚本,而无需在索引脚本中编写计算器代码?

最佳答案

由于您的其他“脚本”是一个 python 模块(.py 文件),您可以导入要运行的函数:

索引.py:

from calculator import multiply  # Import multiply function from calculator.py

category = "math"
print(category)
print("You can use a calculator")
print("What is 43.5 * 7")

#run calculator.py
real_answer = multiply(43.5, 7)

answer = int(input("What is your answer"))

计算器.py

def multiply(a, b)
return a * b

关于python - 如何在我的主脚本中运行另一个脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37426746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com