[英]How to make IDLE python display directly results from a function defined and ran from a .py file
我尝试了很多设置并搜索了相当长的时间。 我将尝试总结我的问题。
我有一个名为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)
得到我的结果。
我希望我能够使我的问题变得简单明了。 谢谢。
您正在打印函数本身,而不是函数调用的输出。 为此,可以将函数输出保存到变量中然后打印,或者直接打印。
第一种方法:
ans = solve(a1,a2)
print(ans)
第二种方法:
print(solve(a1,a2))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.