简体   繁体   English

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

[英]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.

相关问题 如何使.py文件直接打开到空闲 - How to make .py files open directly into Idle Python 上的 `concurrent.futures.ProcessPoolExecutor` 从文件开头而不是定义的函数运行 - `concurrent.futures.ProcessPoolExecutor` on Python is ran from beginning of file instead of the defined function 从IDLE运行时,同一个程序运行良好,但直接运行时,同一个程序运行不正常 - Same Program Runs Fine when ran from IDLE but not when ran directly 如何从IDLE中的.py文件单独运行Python脚本的选择性部分 - How to run a selective part of the Python script individually from .py file in IDLE 如何从Idle的包中运行python文件? - how to run a python file in a package from Idle? 直接从python-requests响应运行时,替换功能不起作用 - Replace function not working when ran directly from python-requests reponse 直接从文件运行python方法/函数 - Running a python method/function directly from a file 如何直接在 django 中的 models.py 文件上进行查询,将结果填充到文件中? - How to make queries directly on the models.py file in django, to fill the fileds with the results? 如何从 python (py) 文件调用 C++ 函数? - How to call a C++ function from a python (py) file? 如何从python中的另一个.py文件调用函数? - How to call function from another .py file in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM