how can we take input in lambda function?
z = lambda a,b,c,d : a+(b-c)*d
y = z(input("enter numbers: "))
print(y)
I expect input of the above expressions.
input returns a string, you need to:
split itmap* unpackingtry this:
z = lambda a, b, c, d: a + (b - c) * d
y = z(*map(int,input("enter numbers: ").split()))
print(y)
example run:
enter numbers: 1 2 3 4
-3
lambda is just a shorthand way of defining a single-expression function.That's neither readable nor flexible, but you can always embed a call to input in the lambda:
z = lambda: int(input('a:')) + int(input('b:'))
ValueError that int will raise if the input isn't valid.As rightly pointed out by Adam, the input function would get you a string. You can split the string and get the comma separated values and then convert them to integer like below
z = lambda l : int(l[0])+(int(l[1])-int(l[2]))*int(l[3])
y = z(input("enter numbers: ").split(','))
print(y)
An alternate way to achieve this would be:
y = lambda stringFromTerminal : [int(eachString) for eachString in stringFromTerminal.split(',')]
x = y(input("enter numbers: "))
print(x[0]+(x[1]-x[2])*x[3])
lambdais usually reserved for key functions and inline definitionsdefstatement.