Python | Calculator using lambda

add = lambda a, b: a + b
subtract = lambda a, b: a - b
multiply = lambda a, b: a * b
divide = lambda a, b: a / b

while True:
    print("""\nEnter
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit""")
    try:
        choice = int(input('\nEnter choice: '))
    except:
        print('Enter integer values only!')
        continue
    if 1<=choice<=5:
        if choice==5:
            print('Exiting...\n')
            break
        numbers = [int(i) for i in input('Enter numbers: ').split()]
        if choice==1:
            print('\nResult is: ', add(numbers[0], numbers[1]), '\n')
            continue
        if choice==2:
            print('\nResult is: ', subtract(numbers[0], numbers[1]), '\n')
            continue
        if choice==3:
            print('\nResult is: ', multiply(numbers[0], numbers[1]), '\n')
            continue
        if choice==4:
            print('\nResult is: ', divide(numbers[0], numbers[1]), '\n')
            continue
    else:
        print('Enter choice between 1 and 5')

Output:

PS C:\CopyAssignment.py"

Enter
1. Addition      
2. Subtraction   
3. Multiplication
4. Division      
5. Exit

Enter choice: ds
Enter integer values only!

Enter
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Enter choice: 1
Enter numbers: 1 2

Result is:  3


Enter
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Enter choice: 4
Enter numbers: 25 4

Result is:  6.25


Enter
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Enter choice: 9
Enter choice between 1 and 5

Enter
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Enter choice: 5
Exiting...

Also Read:

Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you