-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathswitch_case.py
More file actions
60 lines (51 loc) · 977 Bytes
/
switch_case.py
File metadata and controls
60 lines (51 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
"""
Spelling switch/case with a dictionary
"""
"""
A switch/case example:
switch(n) {
case 0:
printf("You typed zero.\n");
break;
case 1:
case 9:
printf("n is a perfect square\n");
break;
case 2:
printf("n is an even number\n");
case 3:
case 5:
case 7:
printf("n is a prime number\n");
break;
case 4:
printf("n is a perfect square\n");
case 6:
case 8:
printf("n is an even number\n");
break;
default:
printf("Only single-digit numbers are allowed\n");
break;
}
"""
def zero():
return "You typed zero.\n"
def sqr():
return "n is a perfect square\n"
def even():
return "n is an even number\n"
def prime():
return "n is a prime number\n"
options = { 0 : zero,
1 : sqr,
4 : sqr,
9 : sqr,
2 : even,
3 : prime,
5 : prime,
7 : prime,
}
print options[2]()
print options[4]()