-5

I have the following Python script

def application():

    from math import exp
    v = lambda t: 3*(t**2)*exp(t**3)
    n = int(input('n: '))
    numerical = trapezoidal(v, 0, 1, n)
    # Compare with exact result
    V = lambda t: exp(t**3)
    exact = V(1) - V(0)
    error = exact - numerical
    print("n = %d: estimation = %.16f, error: %g"%(n, numerical, error))

def trapezoidal(f,a,b,n):
    h = float(b-a)/n #width
    result = 0.5*f(a) + 0.5*f(b)
    for i in range(1,n):
        result += f(a + i*h)
    result *= h
    return result

which is named trapezoidal.py. I cannot understand why when I call it from a terminal (python trapezoidal.py) I get nothing.

Dimitris
  • 503
  • 2
  • 12
  • 4
    Your code defines two functions, but it never _calls_ them... – Aran-Fey Apr 23 '18 at 15:45
  • have you write if __name__ == "__main__": at the end of your script? – Lupanoide Apr 23 '18 at 15:46
  • The above is not really a duplicate - but answers there contain all the information needed to answer this post. @dimitris - please take a look at the linked post. – Lix Apr 23 '18 at 15:47
  • Thanks for the comments. I don't understand the negative votes. Is it something SO obvious? – Dimitris Apr 23 '18 at 15:53
  • Unfortunately I think that you're right @dimitris. The `if __name__ == "__main__"` method is pretty much the way (most) people are introduced to python. I think that the votes are trying to indicate that some research was missing. – Lix Apr 23 '18 at 15:56
  • I don't think that it is "SO obvious" but I do feel that it *should* have been part of the very first introduction to python - how would you execute your code (at such an early stage) if not like this? – Lix Apr 23 '18 at 15:58
  • Sorry, I am not a Python specialist although I have worked a bit with it. I followed the book Programming for Computations – Python of the late H. P. Langtangen (with S. Linge). There if __name__ == "__main__" appears after the authors having already talked about the terminal execution. See page 75 here http://hplgit.github.io/prog4comp/doc/pub/p4c_Python.pdf – Dimitris Apr 23 '18 at 16:04
  • They mention the ` if __name__ == '__main__': application()` only when they talk about modules. – Dimitris Apr 23 '18 at 16:07
  • Ah - I see, well I suppose everyone learns things differently :) Or rather different teachers have different methods of teaching. For your specific case, if you're not `import`ing this file anywhere and are only executing it directly, you don't even need that `if __name__` statement - you can just call the function `application()` at the bottom of the file - it'll do the same thing. – Lix Apr 23 '18 at 18:34

1 Answers1

0

You should define main function

def main():
    # your code here

if __name__ == "__main__":
    main()
Pedro M
  • 21
  • 3
  • 2
    The OP has already defined the two functions that they need - why are you suggesting adding a third? I feel that you may have missed the point behind the question. – Lix Apr 23 '18 at 15:48
  • Because main() function is called when he executes the .py file and he hasn't defined it – Pedro M Apr 23 '18 at 15:50
  • You are however, correct, @pedro - the issue is that your answer doesn't really explain *why* you need this structure and what it does. – Lix Apr 23 '18 at 15:50
  • A function will be executed when it is called - the "magic" part about this is the `__name__ ` variable - the explanation around that is what your answer is missing. – Lix Apr 23 '18 at 15:51