简体   繁体   中英

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.

Inside this script.py I have something like this:

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:

>> 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?

Currently in order to get the results I need to type in the 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM