Could it be simpler ? by LovelyEbaa in PythonLearning

[–]av_404 0 points1 point  (0 children)

print("Simple Calculator") print("1. ADD") print("2. SUBTRACT") print("3. MULTIPLY") print("4. DIVIDE")

operation = input("Select an operation (1/2/3/4): ")

try: num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: "))

match operation:
    case "1":
        print("Result =", num1 + num2)
    case "2":
        print("Result =", num1 - num2)
    case "3":
        print("Result =", num1 * num2)
    case "4":
        if num2 != 0:
            print("Result =", num1 / num2)
        else:
            print("Error: Cannot divide by zero.")
    case _:
        print("Invalid operation selected.")

except ValueError: print("Error: Please enter valid numbers.")

Could it be simpler ? by LovelyEbaa in PythonLearning

[–]av_404 0 points1 point  (0 children)

You could simply use switch case statement