Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Adiv Abramson
6,919 PointsDoes Python have a switch case construct as most other languages? I can't find any documentation on it.
So far all I see are solutions involving dictionary objects (associative arrays). I am trying to find a Python equivalent for
Select Case test_expression
Case condition_1 result_1
Case condition_2 result_2
...
Case condition_n result_n
Case Else result_else
End Select
If only workarounds are currently available, do you think future iterations of the language will correct this egregious deficiency? Thanks
2 Answers
Daniel Reedy
25,284 PointsNo, Python does not have a switch-case statement. The best you'll be able to do is if..elif..elif statements. It's unlikely Python will add this feature, they even provide the if..elif..elif alternative in their control flow docs
Kenneth Love
Treehouse Guest Teacherswitch statements are notoriously difficult to get right, debug, and keep clean/working, so Python purposefully doesn't have them.