Page 560 - ComputerScience_Class_11
P. 560

Create a Menu-driven calculator, which performs varied arithmetic calculations, based on
                Project   3
                               user’s choice.
                 1       # MENU-DRIVEN CALCULATOR

                 2

                 3       print("========== MENU-DRIVEN CALCULATOR ==========")
                 4
                 5       while True:

                 6           print("\nChoose an option:")
                 7           print("1. Addition (+)")

                 8           print("2. Subtraction (-)")
                 9           print("3. Multiplication (*)")

                10           print("4. Division (/)")
                11           print("5. Floor Division (//)")

                12           print("6. Modulus (%)")
                13           print("7. Power (**)")

                14           print("8. Exit")
                15

                16           choice = input("Enter your choice (1-8): ")
                17

                18           if choice == "8":
                19               print("Exiting... Bye!")

                20               break
                21

                22           # For valid operations, take inputs

                23           if choice == "1" or choice == "2" or choice == "3" or choice == "4" or choice
                            == "5" or choice == "6" or choice == "7":
                24               num1 = float(input("Enter first number: "))

                25               num2 = float(input("Enter second number: "))
                26

                27               if choice == "1":
                28                   print("Result:", num1 + num2)

                29
                30               elif choice == "2":

                31                   print("Result:", num1 - num2)
                32






                  558  Touchpad Computer Science (Ver. 3.0)-XI
   555   556   557   558   559   560   561   562   563   564   565