1

How to use lambda by having inputs in python?

I am new to python and having a problem of getting inputs from lambda. I created a function for computation.

FUNCTION

def d_a():
d_a_compute = lambda v_i, t, a: (v_i * t) + (0.5 * a) * (t ** 2)

print("KINEMATIC EQUATIONS")
print ("d_a = Displacement with acceleration")
print ("v_f = Final velocity")
print ("v_f_squared = Final velocity squared")
print ("d = Displacement without acceleration")
print ("t = Time")
print ("a = Acceleration")
print ("v_i = Initial velocity")
print ("===================================")
var = ["d_a", "v_f", "v_f_squared", "d", "t", "a","v_i"]
print ("1.", var[0])
print ("2.", var[1])
print ("3.", var[2])
print ("4.", var[3])
print ("5.", var[4])
print ("6.", var[5])
print ("7.", var[6])
print ("===================================")
num = int(input("Input the number you want to compute: ")) - 1
choice = (var[num])
if choice == "d_a":
    float(input("Enter the initial velocity: "))
    float(input("Enter the time: "))
    float(input("Enter the acceleration: "))

As you can see, I did not call the function because I want to know first how to make my inputs connected to lambda in my function. I want my initial velocity to be connected to v_i, and etc. How can I do this?

Submitted July 24th 2021 by Admin

Answers
0

I guess you can do this ... but you probably should not (see PEP8 )

# def d_a(): < you dont need(or want) this line ...
d_a_compute = lambda v_i, t, a: (v_i * t) + (0.5 * a) * (t ** 2)
...
if choice == "d_a": result = d_a_compute( # <- you need to actually call you lambda float(input("Enter the initial velocity: ")), # v_i float(input("Enter the time: ")), # t float(input("Enter the acceleration: ")) # a ) print("DA = ", result)

as pointed out in the comments its almost certainly more readable to just use a normal function here (in fact it is explicity called out in PEP8 that you should use a normal function definition rather than assign a lambda to a variable)

 def d_a_compute(v_i, t, a): return (v_i * t) + (0.5 * a) * (t ** 2)

instead

Admin | 1 month ago



Relevant Questions