0

Is there a way for make a switch-case stament on python?

I need make a loop with switch, Idk python, someone can tell me how can I make that?

10th Jul 2021, 7:55 PM
Andrés Puello
Andrés Puello - avatar
11 odpowiedzi
+ 1
Andrés Puello In python 3.10 match statement was introduced. It act like a switch statement and it is match with the given pattern in case statement. Here is the documentation, https://docs.python.org/3.10/whatsnew/3.10.html
11th Jul 2021, 4:40 AM
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ - avatar
+ 4
there is only a kind of switch statement since python 3.10: https://towardsdatascience.com/pattern-matching-in-python-3-10-6124ff2079f0
10th Jul 2021, 7:59 PM
visph
visph - avatar
+ 2
As stated, until python 3.10 is released with the match-case the closest we have is the use of a dictionary. https://code.sololearn.com/c9scA7o07x8n/?ref=app
10th Jul 2021, 8:59 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Andrés Puello Here's a really crude possibility: switch = lambda x, y: exec(y[x]) switch(3, { 1: """ print('me') for i in range(8): print(88) """, 2: "print('89')", 3: """ def is_digit(num): ascii = ord(num) return ascii > 47 and ascii < 58 print(is_digit('8')) """, "May": "raise ValueError"}) # Hope this helps
10th Jul 2021, 9:50 PM
Calvin Thomas
Calvin Thomas - avatar
+ 2
Use for loop and inside that use if , elif and else statement.
11th Jul 2021, 3:42 PM
Smit patel
Smit patel - avatar
+ 1
I've found a article on python documentation, but Idk if that is a possibility.
10th Jul 2021, 11:31 PM
Andrés Puello
Andrés Puello - avatar
+ 1
You can use if and while loop instead of switch in python because Python do not have Switch case
12th Jul 2021, 7:30 AM
Akhil Sunny Thogari
Akhil Sunny Thogari - avatar
0
It doesn’t come predifined in python as with js. Unless you define a function. Otherwise its best you use if-else statement
10th Jul 2021, 10:25 PM
Gideon Okike
Gideon Okike - avatar
0
wich article?
10th Jul 2021, 11:33 PM
visph
visph - avatar