0

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.

4
  • Just use a function, lambda is usually reserved for key functions and inline definitions Commented Jul 30, 2019 at 12:50
  • can you please resolve in my code. Commented Jul 30, 2019 at 12:52
  • You should get an error message that explains what you're doing wrong. You only pass it one string argument, but it gives this error: builtins.TypeError: <lambda>() missing 3 required positional arguments: 'b', 'c', and 'd' Commented Jul 30, 2019 at 13:00
  • 1
    A lambda expression defines a regular function, the same as a def statement. Commented Jul 30, 2019 at 13:01

5 Answers 5

6

input returns a string, you need to:

  1. split it
  2. cast each part to int using map
  3. unpack it to different args using * unpacking

try 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
Sign up to request clarification or add additional context in comments.

2 Comments

The important point is there was nothing special about lambda. It's just that you have to pass the right arguments to call a function (defined or lambda).
@KennyOstrom yes, good clarification, lambda is just a shorthand way of defining a single-expression function.
2

A simple example for one variable:

In [1]: z = lambda x : x+1                                                                                                                                                                     

In [2]: z(int(input()))                                                                                                                                                                        
4
Out[2]: 5

In [3]:  

Comments

2

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:'))

1 Comment

This doesn't allow you to recover from the ValueError that int will raise if the input isn't valid.
2

input returns a string, so you'll need to convert that first:

x = input('enter numbers: ') # 1 2 3 4

'1 2 3 4'

# use split
x = input('enter numbers: ')
x = [int(num) for num in x.split()]

# then unpack with the * syntax
c = lambda a, b, c, d: a+(b-c)*d

c(*x)
-3

Comments

1

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])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.