-
Notifications
You must be signed in to change notification settings - Fork 35
/
Exercises_Ch_007.py
205 lines (171 loc) · 4.11 KB
/
Exercises_Ch_007.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# "Python Programming: An Introduction to Computer Science"
# by John Zelle, 3rd Ed.
# Chapter 7: Decision Structures
# End-of-Chapter Exercises
#
# REVIEW QUESTIONS
#
# True/False3
#
# 1. T
# 2. F
# 3. T
# 4. T
# 5. T
# 6. F
# 7. F
# 8. F
# 9. T
# 10. F
#
# Multiple Choice
#
# 1. C
# 2. C
# 3. B
# 4. B
# 5. C
# 6. C
# 7. A
# 8. C
# 9. A
# 10. C
#
# Discussion
#
# 1. The sequences of statements ins a simple decision excecutes when
# the condition true. If it is false, cthe statements are skipped.
# A two-way decision is a simple decision with two parts. If the
# condition is true, the statements are executed. If it is false,
# a different set of statements is excecuted.
# Multi-way decisions specifiy different conditions, which, if true,
# lead to different sets of statements being executd.
# 2. Exception-handling is conditional, similar to if statements. If an
# error occurs in the body of the try statement, Python looks for an
# except clause with a matching error and excecute the handler
# code if an appropriate except is found.
# 3. a. Trees
# Larch
# Done
# b. Trees
# Chestnut
# Done
# c. Spam Please!
# Cheese Shoppe
# Cheddar
# Gouda
# Done
# d. Cheese Shoppe
# Cheddar
# Done
# e. It's a late parrot!
# Trees
# Larch
# Done
# f. Cheese Shoppe
# Cheddar
# Done
#
# PROGRAMMING EXERCISES
# c07ex1.py
# overtime pay
def main():
print("Weekly pay calculator \n")
hours = float(input("Enter hours worked: "))
rate = float(input("hourly wage: "))
if hours <= 40:
pay = hours * wage
else:
pay = 40 * wage + (hours-40) * 1.5 * wage
print("Your week's pay is ${0:0.2f}.".format(pay))
if __name__ == '__main__':
main()
# c07ex2.py
# quiz grade
def main():
print("Quiz grade calculator \n")
score = int(input("Enter quiz score: "))
if score == 5:
grade = "A"
elif score == 4:
grade = "B"
elif score == 3
grade = "C"
elif score == 2
grade = "D"
else:
grade = "F"
print("The grade is ", grade".")
if __name__ == '__main__':
main()
# c07ex3.py
# exam grade
def main():
print("Exam grade calculator \n")
score = int(input("Enter exam score: "))
if score >= 90:
grade = A
elif score >= 80:
grade = B
elif score >= 70:
grade = C
elif score >= 60:
grade = D
else:
grade = F
print("The grade is ", grade".")
if __name__ == '__main__':
main()
# c07ex4.py
# class standing
def main():
print("Class standing calculator\n")
credits = int(input("Enter number of credits: "))
if credits >= 26:
standing = "senior"
elif credits >= 16:
standing = "junior"
elif credits >= 7:
standing = "sophomore"
else:
standing = "freshman"
print("The class standing is", standing".")
if __name__ == '__main__':
main()
# c07ex5.py
# body mass index
def main():
print("BMI calculator \n")
weight = float(input("Enter weight: "))
height = float(input("Enter height: "))
bmi = weight * 720 / height
print = ("Your BMI is ", bmi".")
if bmi < 19:
print("You are below the healthy range.")
elif bmi <= 25:
print("You are in the healthy range.")
else:
print("You are above the healthy range.")
if __name__ == '__main__':
main()
# c07ex6.py
# speeding fine
def main():
print("Speeding fine calculator \n")
speedlimit = int(input("Enter speed limit: "))
clockedspeed = int(input("Enter clocked speed: "))
if clockedspeed <= speedlimit:
print("The speed is legal.")
else:
print("The speed is illegal.")
if clockedspeed > 90:
fine = 5 * (clockedspeed - speedlimit) + 50 + 200
else
fine = 5 * (clockedspeed - speedlimit) + 50
print("The fine is ${0:02f}."format(fine))
if __name__ == '__main__':
main()
# c07ex7.py
# babysitting bill
def main():
print("Babysitting bill calculator \n")