-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch7.decision tree
More file actions
94 lines (75 loc) · 1.76 KB
/
ch7.decision tree
File metadata and controls
94 lines (75 loc) · 1.76 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
chapter 7 Decision Structures
<relop> : relational operator
1) Simple Decisions
In a program designed to be either imported (without running) or run directly,
the call to ma in at the bottom must be made conditional. A simple decision
should do the trick:
[imported]
if <condition>:
<body>
[run directly]
if <condition>:
main ( )
Whenever a module is imported, Python creates a special variable, __name__ ,
inside that module and assigns it a string representing the module's name.
>>> import math
>>> math . __name__
'math'
However, when Python code is being run directly (not imported), Python
sets the value of _..name__ to be '__main__ ' .
>>> __name__
, __ main __ ,
2) Two-Way Decisions
if <condition> :
<statements>
else :
<statements>
3) Multi-Way Decisions
if < condition1 > :
< case 1 statements>
elif < condition2> :
< case2 statements>
elif < condition3> :
< case3 statements>
...
else :
<default statements>
4. Exception Handling
try :
<body >
except <ErrorType> :
<handler>
5. Study in Design: Max of Three
Strategy 1: Compare Each to All
if x1 >= x2 and x1 >= x3 :
ma xval = x1
el if x2 >= x1 and x2 >= x3 :
ma xval - x2
el se :
ma xval - x3
Strategy 2: Decision Tree
if x1 >= x2 :
if x1 >= x3 :
maxval - x1
el se :
ma xval - x3
el se :
if x2 >= x3 :
ma xval - x2
el se :
maxval - x3
Strategy 3: Sequential Processing
ma xval = x1
if x2 > maxval:
ma xval = x2
if x3 > ma xval:
ma xval = x3
Strategy 4: Use Python
d ef ma in ( ):
x1, x2, x3 = eval ( input (" Pl ea se enter three val ues: " ) )
print (" T he la rgest val ue is", ma x (x1, x2, x3) )
* Don't be afraid to step back and think
about the overarching problem. Similarly, when designing programs, you
should always have an eye toward making your program more generally
useful.
* Don't reinvent the wheel.