+ 1
Is there "switch case" structure in Python?
2 Respuestas
+ 4
We can use a dictionary to map cases to their functionality. Here, we define a function week() to tell us which day a certain day of the week is. A switcher is a dictionary that performs this mapping.
>>> def week(i):
switcher={
0:'Sunday',
1:'Monday',
2:'Tuesday',
3:'Wednesday',
4:'Thursday',
5:'Friday',
6:'Saturday'
}
return switcher.get(i,"Invalid day of week")
Now, we make calls to week() with different values.
Example :
>>> week(2)
‘Tuesday’
>>> week(0)
‘Sunday’
>>> week(7)
‘Invalid day of week’
>>> week(4.5)
‘Invalid day of week’
0
Thanks Neha Gupta