如何使IDLE python显示直接来自定义的函数并从.py文件运行

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

我已经尝试了很多设置和搜索很长一段时间。我会试着总结一下我的问题。

我有一个名为script.py的文件。

在这个script.py里面我有这样的东西:

import math
import numpy as np
from numpy import matrix

#Inserting variables:
A=float(input("insert position 1: "))
K=float(input("insert position 2: "))

#Doing some math:
a1=A*K
a2=A/K

#Defining a funtion:
def solve(var1,var2)
#This function uses numpy and math and handles matrices.
#I am not putting it to save space and make my problem clear

#Calling the funtion:
solve(a1,a2)
print (solve)
#The values of a1 and a2 are the once I calculated previously

然后我按“运行模块”运行script.py,它显示:

>> insert position 1:

>> insert position 2:

我插入值,然后显示:

<function solve at 0x000000000A0C1378>

我该怎么做才能使python shell直接显示结果?

目前为了获得结果,我需要输入python shell

>> solve(a1,a2)

得到我的结果。

我希望我能够使我的问题清晰简单。谢谢。

python python-3.x function numpy
1个回答
0
投票

您正在打印函数本身,而不是函数调用的输出。要实现此目的,请将功能输出保存到变量然后打印,或者直接打印。

第一种方法:

ans = solve(a1,a2)
print(ans)

第二种方法:

print(solve(a1,a2))
© www.soinside.com 2019 - 2024. All rights reserved.