Home>

I'm studying Python, but when I execute the following code, I don't get any errors and it just ends.
Where should I fix or add?

Corresponding source code
import math
def ex3_4 (vp, vs, t):
    vp = 6
    vs = 3
    t = 2
    p = (vp * vs)/(vp-vs) * t
    return (p)
Supplementary information (FW/tool version, etc.)

I'm using python3.9

  • Answer # 1

    Problem 1
    A python function returns a value when called.
    Asuka_wataki's program only defines a function, it doesn't call it.

    Problem 2
    The python function arguments, in this case vp, vs and t, give the value when called.

    Problem 3
    When executing a program contained in a file, it will not be output unless a print statement is used.

    Problem 4
    This program does not require import math.

    If you modify these, it will be as follows.

    def ex3_4 (vp, vs, t):
        p = (vp * vs)/(vp-vs) * t
        return (p)
    print (ex3_4 (6, 3, 2))

    When executed in interactive mode, it will be as follows.

    $python
    Python 3.8.3 (default, Jul 2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>def ex3_4 (vp, vs, t):
    ... p = (vp * vs)/(vp-vs) * t
    ... return (p)
    ...
    >>>print (ex3_4 (6, 3, 2))
    12.0


    When executed in command execution mode, it will be as follows.

    $python ex3_4.py
    12.0

  • Answer # 2

    It has been calculated.
    def ex3_4 (vp, vs, t) is
    We just define a function called ex3_4 with vp, vs, t as variables, so we need to call that function.

    Please check the following additions.

    import math
    def ex3_4 (vp, vs, t):
    # vp = 6
    # vs = 3
    # t = 2
        p = (vp * vs)/(vp-vs) * t
        return (p)
    ans = ex3_4 (6,3,2) #function call
    print (ans) #Display result


    By the way, vp, vs, t can be operated from the outside, so I commented them out.
    If you give a number in ex3_4 (,,), it will return the number.