[英]How to make IDLE python display directly results from a function defined and ran from a .py file
I have tried many set and search quite some time. 我尝试了很多设置并搜索了相当长的时间。 I'll try to summarize to my problem.
我将尝试总结我的问题。
I a have a file I name script.py. 我有一个名为script.py的文件。
Inside this script.py I have something like this: 在此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
Then I press "run module" to run script.py, it shows: 然后我按“运行模块”运行script.py,它显示:
>> insert position 1:
>> insert position 2:
I insert the values and then it shows: 我插入值,然后显示:
<function solve at 0x000000000A0C1378>
What can I do to make the python shell display the result directly? 如何使python shell直接显示结果?
Currently in order to get the results I need to type in the python shell 目前,为了获得结果,我需要在python shell中键入
>> solve(a1,a2)
to have my result. 得到我的结果。
I hope I was able to make my problem clear and simple. 我希望我能够使我的问题变得简单明了。 Thanks.
谢谢。
You are printing the function itself, not the output from the function call. 您正在打印函数本身,而不是函数调用的输出。 To achieve this, either save the function output to a variable then print, or just print straight away.
为此,可以将函数输出保存到变量中然后打印,或者直接打印。
1st Method: 第一种方法:
ans = solve(a1,a2)
print(ans)
2nd Method: 第二种方法:
print(solve(a1,a2))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.