+ 3

Control Flow

What does match case do in Python?

26th Nov 2024, 11:38 AM
Byrone Songa
Byrone Songa - avatar
7 odpowiedzi
+ 8
it known as switch statements, it work like if/else.
26th Nov 2024, 1:11 PM
Alhaaz
Alhaaz - avatar
+ 6
Alhaaz , your comment that python `match ... case` can not handle float values is *not* correct. it requires python 3.10 +, (the current version of sololearn is 3.9.18) https://sololearn.com/compiler-playground/c3TAJXE2T92P/?ref=app
26th Nov 2024, 9:50 PM
Lothar
Lothar - avatar
+ 4
When you have to compare something several times, you can write a bunch of IF statements. Match Case is a simple way to do that. You can always do this with IF statements. But it's a little easier to read like this. Look at this function: def http_error(status): match status: case 400: return "Bad request" case 404: return "Not found" case 418: return "I'm a teapot"
26th Nov 2024, 1:28 PM
Jerry Hobby
Jerry Hobby - avatar
+ 3
Okay 👍👍. Thank you 🙏🙏🙏🙏 for the feedback.
27th Nov 2024, 8:42 AM
Byrone Songa
Byrone Songa - avatar
+ 2
Thank you 🙏🙏🙏🙏🙏
26th Nov 2024, 1:30 PM
Byrone Songa
Byrone Songa - avatar
+ 2
yes, Sololearn's Python version is not updated yet, so you cannot use it here... you get a syntax error instead.
27th Nov 2024, 12:07 AM
Bob_Li
Bob_Li - avatar
+ 2
It helps to align the code on it
27th Nov 2024, 7:20 PM
Precious
Precious - avatar