Johnny Doe
Johnny Doe

Reputation: 35

physics equation in python

I want to make a program that can calculate physics equation where the user enters different parameters: I know its simple like:

v=5
t=7
s=v*t
print(s)

its only calculating s = v*t; however, when I want the equation of v if show me an error. Hard coding v = s/t gives me the correct result:

s=5
t=7
v=s/t
print(v)

I want an equation that can solve with different user input; that is if user inputs the v and t , the equation will return s = v*t and if the user inputs s and t the equation will return v = s/v.

Upvotes: 1

Views: 4868

Answers (4)

alberto987
alberto987

Reputation: 1

def h2(v,d,t):
    title_ar = 'السرعة'
    title_en = 'speed'
    if v=='':
        print("v= ","%.2f"% (d/t),"m/s")
    elif d=='':
        print("d= ","%.2f"% (v*t),"m")
    elif t=='':
        print("t= ","%.2f"% (d/v),"s")
    else:
        print("please write all data.")

h2('', 2,6)
h2(70,'',8)
h2(25,3,'')

Upvotes: 0

snibbets
snibbets

Reputation: 1145

I'm not sure if this is what you want, but you could define three separate functions, one for each variable. For example,

def s(v, t):
    return v*t

def v(s, t):
    return s/t

def t(s, v):
    return s/v

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881403

Assuming you're talking about the zero acceleration "svt" equations (or even the constant acceleration "suvat" ones, should you have more complex requirements), it's a simple matter of detecting what's unknown, and then filling in the gaps.

The following code provides a function that will do this along with some test code so you can see it in action:

# Returns a 3-tuple containing [displacement(s), velocity(v), time(t)],
#    based on the existence of at least two of those.
# If all three are given, the displacement is adjusted so that the
#    equation 's = vt' is correct.

def fillInSvt(s = None, v = None, t = None):
    # Count the unknowns, two or more means no unique solution.

    noneCount = sum(x is None for x in [s, v, t])
    if noneCount > 1: return [None, None, None]

    # Solve for single unknown (or adjust s if none).

    if noneCount == 0 or s is None: return [v*t, v, t]
    if v is None: return [s, s/t, t]
    return [s, v, s/v]

# Test code.

print(fillInSvt(99,4,6))         # Show case that adjusts s.

print(fillInSvt(None,4,6))       # Show cases that fill in unknown.
print(fillInSvt(24,None,6))
print(fillInSvt(24,4,None))

print(fillInSvt(24,None,None))   # Show "not enough info" cases.
print(fillInSvt(None,4,None))
print(fillInSvt(None,None,6))
print(fillInSvt(None,None,None))

The output shows that the tuple is filled in in all cases where there's a unique solution:

[24, 4, 6]
[24, 4, 6]
[24, 4.0, 6]
[24, 4, 6.0]
[None, None, None]
[None, None, None]
[None, None, None]
[None, None, None]

Upvotes: 1

Reblochon Masque
Reblochon Masque

Reputation: 36662

You could use key word arguments:

def solve_equation(v=None, t=None, s=None):
    if v is not None and t is not None:
        return v * t   # s case
    elif s is not None and t:  # t not None and not 0            
        return s / t   # v case
    else:
        raise ValueError   #"t should be defined or not zero"


print(solve_equation(v=10, t=2))
print(solve_equation(s=2, t=7))

Output:

20
0.2857142857142857

Note that if you are using python 2, floats must be passed.

Upvotes: 4

Related Questions