switch-statement

How to create a switch case in python with the cases being intervals?

 ̄綄美尐妖づ 提交于 2020-12-04 05:54:52
问题 I'm new to python, and I'd like to create a switch case where the cases can have intervals as condition, like: switch = { 1..<21: do one stuff, 21...31: do another } How can I achieve this result? 回答1: As it looks like you already tried, the obvious way of implementing a switch structure in Python is using a dictionary. In order to support intervals , you could implement your own dict class: class Switch(dict): def __getitem__(self, item): for key in self.keys(): # iterate over the intervals

How to create a switch case in python with the cases being intervals?

半腔热情 提交于 2020-12-04 05:54:46
问题 I'm new to python, and I'd like to create a switch case where the cases can have intervals as condition, like: switch = { 1..<21: do one stuff, 21...31: do another } How can I achieve this result? 回答1: As it looks like you already tried, the obvious way of implementing a switch structure in Python is using a dictionary. In order to support intervals , you could implement your own dict class: class Switch(dict): def __getitem__(self, item): for key in self.keys(): # iterate over the intervals

Issue with switch statement

与世无争的帅哥 提交于 2020-12-04 01:45:15
问题 Let's see the problem by code: code-1 #include <stdio.h> int main(int argc, char *argv[]) { int a =1; switch (a) { printf("This will never print\n"); case 1: printf(" 1"); break; default: break; } return 0; } Here the printf() statement is never going to execute - see http://codepad.org/PA1quYX3. But code-2 #include <stdio.h> int main(int argc, char *argv[]) { int a = 1; switch (a) { int b; case 1: b = 34; printf("%d", b); break; default: break; } return 0; } Here int b is going to be

Issue with switch statement

邮差的信 提交于 2020-12-04 01:42:53
问题 Let's see the problem by code: code-1 #include <stdio.h> int main(int argc, char *argv[]) { int a =1; switch (a) { printf("This will never print\n"); case 1: printf(" 1"); break; default: break; } return 0; } Here the printf() statement is never going to execute - see http://codepad.org/PA1quYX3. But code-2 #include <stdio.h> int main(int argc, char *argv[]) { int a = 1; switch (a) { int b; case 1: b = 34; printf("%d", b); break; default: break; } return 0; } Here int b is going to be

Issue with switch statement

吃可爱长大的小学妹 提交于 2020-12-04 01:40:30
问题 Let's see the problem by code: code-1 #include <stdio.h> int main(int argc, char *argv[]) { int a =1; switch (a) { printf("This will never print\n"); case 1: printf(" 1"); break; default: break; } return 0; } Here the printf() statement is never going to execute - see http://codepad.org/PA1quYX3. But code-2 #include <stdio.h> int main(int argc, char *argv[]) { int a = 1; switch (a) { int b; case 1: b = 34; printf("%d", b); break; default: break; } return 0; } Here int b is going to be