4 Réponses
+ 4
Here is a neat way using a dictionary:
choices = {'a': 1, 'b': 2}
result = choices.get(key, 'default')
You can always use if/elif/else:
if x == 'a':
# Do the thing
elif x == 'b':
# Do the other thing
if x in 'bc':
# Fall-through by not using elif, but now the default case includes case 'a'
elif x in 'xyz':
# Do yet another thing
else:
# Do the default
+ 4
if:
elif:
(repeat as much as you want)
else:
Sorry bro Python is too simple, no other way.
Others include
if:
else:
if:
else:
if:
else:
(not advisable)
+ 2
"elif" is same as "else-if' in CPP, there might be some easier methods.
0
He is asking for Python mu friend