如何让IDLE Python直接显示从.py文件中定义和运行的函数的结果

2024-07-07 06:49:08 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我有一个文件名为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>

如何使pythonshell直接显示结果

目前,为了获得结果,我需要在pythonshell中键入

>> solve(a1,a2)

我想知道我的结果

我希望我能把我的问题说得清楚简单。谢谢


Tags: andpyimportnumpya2inputa1script
1条回答
网友
1楼 · 发布于 2024-07-07 06:49:08

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

第一种方法:

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

第二种方法:

print(solve(a1,a2))

相关问题 更多 >