【问题标题】:How to make IDLE python display directly results from a function defined and ran from a .py file如何使 IDLE python 直接显示从 .py 文件定义和运行的函数的结果
【发布时间】:2018-02-26 04:04:47
【问题描述】:

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

我有一个名为 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】:

    您正在打印函数本身,而不是函数调用的输出。要实现这一点,要么将函数输出保存到变量中然后打印,要么直接打印。

    第一种方法:

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

    第二种方法:

    print(solve(a1,a2))
    

    【讨论】:

      猜你喜欢
      • 2019-05-08
      • 2016-08-09
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      相关资源
      最近更新 更多