Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Switch case or match case?
#1
Hello,

I hate to ask again but i am trying to introduce a switch statement to get rid of if, else statements.

This is the original code and i would like to get rid of the if and elif. I have been reading a few posts about it saying that it is similar to switch case default in C#. But it was introduced as match case in recent versions of Python. Not so sure what to do with the implementation.

# Program make a simple calculator

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    return x / y


print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

while True:
   
    choice = input("Enter choice(1/2/3/4): ")

    # check if choice is one of the four options
    if choice in ('1', '2', '3', '4'):
        num1 = float(input("Enter first number: "))
        num2 = float(input("Enter second number: "))

        if choice == '1':
            print(num1, "+", num2, "=", add(num1, num2))

        elif choice == '2':
            print(num1, "-", num2, "=", subtract(num1, num2))

        elif choice == '3':
            print(num1, "*", num2, "=", multiply(num1, num2))

        elif choice == '4':
            print(num1, "/", num2, "=", divide(num1, num2))
        
        # check if user wants another calculation
        # break the while loop if answer is no
        next_calculation = input("Let's do next calculation? (yes/no): ")
        if next_calculation == "no":
          break
    
    else:
        print("Invalid Input")
I started with this :

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    return x / y


print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")


    # take input from the user
def switch():
    choice = input("Enter choice(1/2/3/4): ")

    # check if choice is one of the four options
    
    num1 = float(input("Enter first number: "))
    num2 = float(input("Enter second number: "))

        case == "1":
            print(num1, "+", num2, "=", add(num1, num2))

        case == "2":
            print(num1, "-", num2, "=", subtract(num1, num2))

        case == "3":
            print(num1, "*", num2, "=", multiply(num1, num2))

        case == "4":
            print(num1, "/", num2, "=", divide(num1, num2))
        
        # check if user wants another calculation
        # break the while loop if answer is no
        next_calculation = input("Let's do next calculation? (yes/no): ")
        if next_calculation == "no":
          break
    
        else:
          print("Invalid Input")
Any inputs on this? Not sure how to implement this.

TY
Reply


Messages In This Thread
Switch case or match case? - by Frankduc - Jan-19-2022, 06:14 PM
RE: Switch case or match case? - by deanhystad - Jan-19-2022, 07:00 PM
RE: Switch case or match case? - by Frankduc - Jan-19-2022, 07:05 PM
RE: Switch case or match case? - by menator01 - Jan-19-2022, 07:50 PM
RE: Switch case or match case? - by Frankduc - Jan-19-2022, 08:09 PM
RE: Switch case or match case? - by deanhystad - Jan-19-2022, 07:37 PM
RE: Switch case or match case? - by snippsat - Jan-20-2022, 06:28 AM
RE: Switch case or match case? - by Frankduc - Jan-20-2022, 01:22 PM
RE: Switch case or match case? - by snippsat - Jan-20-2022, 01:32 PM
RE: Switch case or match case? - by Frankduc - Jan-20-2022, 01:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Good way to ignore case when searching elements? Winfried 1 340 Apr-25-2024, 12:39 PM
Last Post: menator01
  switch case not working Username0089098 1 747 Apr-09-2023, 05:49 AM
Last Post: buran
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 1,023 Feb-15-2023, 05:34 PM
Last Post: zsousa
  What colon (:) in Python mean in this case? Yapwc 4 2,335 Dec-28-2022, 04:04 PM
Last Post: snippsat
  Merging two Data Frame on a special case piku9290dgp 0 1,125 Mar-02-2022, 10:43 AM
Last Post: piku9290dgp
  best way to use switch case? korenron 8 3,131 Aug-18-2021, 03:16 PM
Last Post: naughtyCat
  Logstash - sending Logstash messages to another host in case of Failover in python Suriya 0 1,725 Jul-27-2021, 02:02 PM
Last Post: Suriya
  Help: write 'case' with Python ICanIBB 2 1,936 Jan-27-2021, 09:39 PM
Last Post: Larz60+
  Case sensitive checks kam_uk 2 2,090 Nov-30-2020, 01:25 AM
Last Post: bowlofred
  case transparent names in configpaser Skaperen 4 2,708 Oct-02-2020, 01:16 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020