Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Switch case or match case?
#5
(Jan-19-2022, 07:00 PM)deanhystad Wrote: Are you trying to use the new structural pattern matching introduced in Python 3.10? As far as I know that is the only Python feature that uses "case" as a keyword. I have not installed 3.10 yet, but I believe the code would look like this:
while True:
    op = input(Select operation +, -, *, /: ")
    if op not in "+-*/":
        print("Invalid Input")
    else:
        num1 = float(input("Enter first number: "))
        num2 = float(input("Enter second number: "))
        match choice:
            case ['+']:
                print(num1, "+", num2, "=", num1 + num2)
            case ['-']:
                print(num1, "-", num2, "=", num1 - num2)
            case ['*']:
                print(num1, "*", num2, "=", num1 * num2)
            case ['/']:
                print(num1, "/", num2, "=", num1 / num2)
 
    if input("Let's do next calculation? (yes/no): ") == "no":
        break
I look forward to using this new feature. If I had to solve this problem without structured pattern matching I would probably fall back on using a dictionary.
operations = {
    "+": lambda a, b: print(f"{a} + {b} = {a+b}"),
    "-": lambda a, b: print(f"{a} - {b} = {a-b}"),
    "*": lambda a, b: print(f"{a} * {b} = {a*b}"),
    "/": lambda a, b: print(f"{a} / {b} = {a/b}"),
}

while True:
    op = input(f"Select operation {', '.join(operations.keys())}: ")
    if op not in operations:
        print("Invalid Input")
    else:
        num1 = float(input("Enter first number: "))
        num2 = float(input("Enter second number: "))
        operations[op](num1, num2)

    if input("Let's do next calculation? (yes/no): ") == "no":
        break

I had to tweak the code a little to get it to work.

while True:
    op = input("Select operation +, -, *, /: ")
    if op not in "+-*/":
        print("Invalid Input")
    else:
        num1 = float(input("Enter first number: "))
        num2 = float(input("Enter second number: "))
        match op:
            case '+':
                print(num1, "+", num2, "=", num1 + num2)
            case '-':
                print(num1, "-", num2, "=", num1 - num2)
            case '*':
                print(num1, "*", num2, "=", num1 * num2)
            case '/':
                print(num1, "/", num2, "=", num1 / num2)

    if input("Let's do next calculation? (yes/no): ") == "no":
        break
Output:
Select operation +, -, *, /: + Enter first number: 1 Enter second number: 1 1.0 + 1.0 = 2.0 Let's do next calculation? (yes/no): no
Frankduc and BashBedlam like this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


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 228 Apr-25-2024, 12:39 PM
Last Post: menator01
  switch case not working Username0089098 1 718 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 987 Feb-15-2023, 05:34 PM
Last Post: zsousa
  What colon (:) in Python mean in this case? Yapwc 4 2,232 Dec-28-2022, 04:04 PM
Last Post: snippsat
  Merging two Data Frame on a special case piku9290dgp 0 1,104 Mar-02-2022, 10:43 AM
Last Post: piku9290dgp
  best way to use switch case? korenron 8 3,079 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,701 Jul-27-2021, 02:02 PM
Last Post: Suriya
  Help: write 'case' with Python ICanIBB 2 1,911 Jan-27-2021, 09:39 PM
Last Post: Larz60+
  Case sensitive checks kam_uk 2 2,035 Nov-30-2020, 01:25 AM
Last Post: bowlofred
  case transparent names in configpaser Skaperen 4 2,667 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