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