- 1
Is this correct lambda expression to find a even or odd number
number = int(input("Enter a number >> ")) odd_even = lambda : "Even Number" if number % 2 == 0 else "Odd Number" print(odd_even())
4 Respuestas
+ 2
Why don't you put your function into Code Playground and test it?
+ 5
🄼🄼🄷🄺
Yes the lambda expression is correct.
But I would say it is better if you pass 'number' as a parameter to the lambda function:
https://code.sololearn.com/cPT7YbpRZfzO/?ref=app
+ 4
Four downvotes? For my answer? I thought my comment to be the most natural recommendation, and there was definitely no harm intended!
Normally you'd put a code here after you tried it but it puts out awkward results or gives errors and you don't understand what's wrong. Then you'd ask: 'What's wrong with this?'
But 'is this lambda expression correct?' is easily to be answered by just feeding numbers and check if they get evaluated correctly.
0
n=int(input())
odd_even=lambda n:"Even number" if(n%2==0) else "Odd number"
print(odd_even(n))