4
4
"metadata" : {
5
5
"colab" : {
6
6
"provenance" : [],
7
- "authorship_tag" : " ABX9TyNvfjEBR+UU23lMYneAQauU " ,
7
+ "authorship_tag" : " ABX9TyPs/xaIgnXS0KLEnKKFfvRH " ,
8
8
"include_colab_link" : true
9
9
},
10
10
"kernelspec" : {
40
40
{
41
41
"cell_type" : " code" ,
42
42
"source" : [
43
+ " # we can not brute force the code to repeat a programming statement.\n " ,
44
+ " # we got to be smarter in doing the repititions.\n " ,
45
+ " print(\" testing\" )\n " ,
46
+ " print(\" testing\" )\n " ,
47
+ " print(\" testing\" )\n " ,
48
+ " print(\" testing\" )\n " ,
49
+ " print(\" testing\" )\n " ,
50
+ " print(\" testing\" )\n " ,
51
+ " print(\" testing\" )\n " ,
52
+ " print(\" testing\" )\n " ,
53
+ " print(\" testing\" )\n " ,
54
+ " print(\" testing\" )\n " ,
55
+ " print(\" testing\" )\n " ,
43
56
" print(\" testing\" )"
44
57
],
45
58
"metadata" : {
46
59
"colab" : {
47
60
"base_uri" : " https://localhost:8080/"
48
61
},
49
62
"id" : " JAo-WnV7DQmr" ,
50
- "outputId" : " 393c1a56-8062-4685-a7d3-d0348b7217de "
63
+ "outputId" : " d4da6011-0801-4b27-b138-2f0cbdfb0bd4 "
51
64
},
52
- "execution_count" : 3 ,
65
+ "execution_count" : 4 ,
53
66
"outputs" : [
54
67
{
55
68
"output_type" : " stream" ,
56
69
"name" : " stdout" ,
57
70
"text" : [
71
+ " testing\n " ,
72
+ " testing\n " ,
73
+ " testing\n " ,
74
+ " testing\n " ,
75
+ " testing\n " ,
76
+ " testing\n " ,
77
+ " testing\n " ,
78
+ " testing\n " ,
79
+ " testing\n " ,
80
+ " testing\n " ,
81
+ " testing\n " ,
58
82
" testing\n "
59
83
]
60
84
}
61
85
]
86
+ },
87
+ {
88
+ "cell_type" : " code" ,
89
+ "source" : [
90
+ " # Basic example of while loop\n " ,
91
+ " n = 5\n " ,
92
+ " while (n > 0) :\n " ,
93
+ " print(n)\n " ,
94
+ " n = n-1\n " ,
95
+ " print(\" Take Off!\" )\n " ,
96
+ " print(n)"
97
+ ],
98
+ "metadata" : {
99
+ "colab" : {
100
+ "base_uri" : " https://localhost:8080/"
101
+ },
102
+ "id" : " HSbSycexG7IK" ,
103
+ "outputId" : " bc3028b6-a1ca-4c4e-c18d-adf9529b1bf4"
104
+ },
105
+ "execution_count" : 5 ,
106
+ "outputs" : [
107
+ {
108
+ "output_type" : " stream" ,
109
+ "name" : " stdout" ,
110
+ "text" : [
111
+ " 5\n " ,
112
+ " 4\n " ,
113
+ " 3\n " ,
114
+ " 2\n " ,
115
+ " 1\n " ,
116
+ " Take Off!\n " ,
117
+ " 0\n "
118
+ ]
119
+ }
120
+ ]
121
+ },
122
+ {
123
+ "cell_type" : " markdown" ,
124
+ "source" : [
125
+ " ### 4.1 The Bug Collector Problem\n " ,
126
+ " A bug collector collects bugs every day for five days. Write a program that keeps a running total\n " ,
127
+ " of the number of bugs collected during the five days. The loop should ask for the number of\n " ,
128
+ " bugs collected for each day, and when the loop is finished, the program should display the total\n " ,
129
+ " number of bugs collected."
130
+ ],
131
+ "metadata" : {
132
+ "id" : " 3HVcaXirKiMs"
133
+ }
134
+ },
135
+ {
136
+ "cell_type" : " code" ,
137
+ "source" : [
138
+ " day_count = 1\n " ,
139
+ " total_bugs = 0\n " ,
140
+ " \n " ,
141
+ " # while (day_count < 6): # ok\n " ,
142
+ " # while (day_count <= 5): # ok\n " ,
143
+ " # while (day_count < 5): # not ok; One-off defect\n " ,
144
+ " \n " ,
145
+ " while (day_count <= 5): \n " ,
146
+ " print(\" Day Number: \" , day_count)\n " ,
147
+ " bug_count = input(\" Enter the bugs you collected: \" )\n " ,
148
+ " bug_count = int(bug_count)\n " ,
149
+ " total_bugs = total_bugs + bug_count\n " ,
150
+ " day_count = day_count + 1\n " ,
151
+ " \n " ,
152
+ " # output\n " ,
153
+ " print(\" Total number of bugs collected: \" , total_bugs)"
154
+ ],
155
+ "metadata" : {
156
+ "id" : " OGEHZllNKhiU"
157
+ },
158
+ "execution_count" : null ,
159
+ "outputs" : []
160
+ },
161
+ {
162
+ "cell_type" : " code" ,
163
+ "source" : [
164
+ " # With minor enhancement\n " ,
165
+ " # FInal version for programming assignment 2.1\n " ,
166
+ " day_count = 1\n " ,
167
+ " total_bugs = 0\n " ,
168
+ " \n " ,
169
+ " # while (day_count < 6): # ok\n " ,
170
+ " # while (day_count <= 5): # ok\n " ,
171
+ " # while (day_count < 5): # not ok; One-off defect\n " ,
172
+ " \n " ,
173
+ " while (day_count <= 5): \n " ,
174
+ " #print(\" Day Number: \" , day_count)\n " ,
175
+ " bug_count = input(\" Enter the bugs you collected on day \" + str(day_count) + \" : \" )\n " ,
176
+ " bug_count = int(bug_count)\n " ,
177
+ " total_bugs = total_bugs + bug_count\n " ,
178
+ " day_count = day_count + 1\n " ,
179
+ " \n " ,
180
+ " # output\n " ,
181
+ " print(\" Total number of bugs collected: \" , total_bugs)"
182
+ ],
183
+ "metadata" : {
184
+ "id" : " grCtpF80Nh90"
185
+ },
186
+ "execution_count" : null ,
187
+ "outputs" : []
188
+ },
189
+ {
190
+ "cell_type" : " markdown" ,
191
+ "source" : [
192
+ " ### 4.2. Calories Burned\n " ,
193
+ " Running on a particular treadmill you burn 4.2 calories per minute. Write a program that uses a\n " ,
194
+ " loop to display the number of calories burned after 10, 15, 20, 25, and 30 minutes."
195
+ ],
196
+ "metadata" : {
197
+ "id" : " HVzZrxAmPGFG"
198
+ }
199
+ },
200
+ {
201
+ "cell_type" : " code" ,
202
+ "source" : [
203
+ " # declare and initialize variables\n " ,
204
+ " CAL_PER_MINUTE = 4.2\n " ,
205
+ " starting_minute = 10\n " ,
206
+ " \n " ,
207
+ " # setup for loop\n " ,
208
+ " while (starting_minute < 31):\n " ,
209
+ " calories_burned = starting_minute * CAL_PER_MINUTE\n " ,
210
+ " print(\" Number of calories burned after\" , starting_minute, \" minutes: \" , calories_burned)\n " ,
211
+ " starting_minute = starting_minute + 5\n " ,
212
+ " \n " ,
213
+ " # output\n " ,
214
+ " print(\" Done!\" )\n " ,
215
+ " \n "
216
+ ],
217
+ "metadata" : {
218
+ "id" : " whepmowLPSk4"
219
+ },
220
+ "execution_count" : null ,
221
+ "outputs" : []
222
+ },
223
+ {
224
+ "cell_type" : " markdown" ,
225
+ "source" : [
226
+ " Number of calories burned after 10 minutes: 42.0\n " ,
227
+ " Number of calories burned after 15 minutes: 63.0\n " ,
228
+ " Number of calories burned after 20 minutes: 84.0\n " ,
229
+ " Number of calories burned after 25 minutes: 105.0\n " ,
230
+ " Number of calories burned after 30 minutes: 126.0\n " ,
231
+ " Done!\n " ,
232
+ " \n " ,
233
+ " ===============>\n " ,
234
+ " \n " ,
235
+ " Minutes Calories\n " ,
236
+ " 10 42.0\n " ,
237
+ " 15 63.0\n " ,
238
+ " 20 84.0\n " ,
239
+ " 25 105.0\n " ,
240
+ " 30 126.0"
241
+ ],
242
+ "metadata" : {
243
+ "id" : " 2b43Zt64QVQg"
244
+ }
245
+ },
246
+ {
247
+ "cell_type" : " code" ,
248
+ "source" : [
249
+ " # declare and initialize variables\n " ,
250
+ " CAL_PER_MINUTE = 4.2\n " ,
251
+ " starting_minute = 10\n " ,
252
+ " \n " ,
253
+ " # setup for loop\n " ,
254
+ " while (starting_minute < 31):\n " ,
255
+ " calories_burned = starting_minute * CAL_PER_MINUTE\n " ,
256
+ " print(\" Number of calories burned after\" , starting_minute, \" minutes: \" , calories_burned)\n " ,
257
+ " starting_minute = starting_minute + 5\n " ,
258
+ " \n " ,
259
+ " # output\n " ,
260
+ " print(\" Done!\" )\n " ,
261
+ " \n "
262
+ ],
263
+ "metadata" : {
264
+ "id" : " jHILbJLVQRBn"
265
+ },
266
+ "execution_count" : null ,
267
+ "outputs" : []
268
+ },
269
+ {
270
+ "cell_type" : " code" ,
271
+ "source" : [
272
+ " # declare and initialize variables\n " ,
273
+ " CAL_PER_MINUTE = 4.2\n " ,
274
+ " starting_minute = 10\n " ,
275
+ " \n " ,
276
+ " # setup for loop\n " ,
277
+ " print(\" Minutes Calories\" )\n " ,
278
+ " while (starting_minute < 31):\n " ,
279
+ " calories_burned = starting_minute * CAL_PER_MINUTE\n " ,
280
+ " print(starting_minute, \" \" , calories_burned)\n " ,
281
+ " starting_minute = starting_minute + 5\n " ,
282
+ " \n " ,
283
+ " # output\n " ,
284
+ " print(\" Done!\" )"
285
+ ],
286
+ "metadata" : {
287
+ "id" : " JJ6rovNIQ6_B"
288
+ },
289
+ "execution_count" : null ,
290
+ "outputs" : []
291
+ },
292
+ {
293
+ "cell_type" : " markdown" ,
294
+ "source" : [
295
+ " ### 4.7. Pennies for Pay\n " ,
296
+ " Write a program that calculates the amount of money a person would earn over a period of time\n " ,
297
+ " if his or her salary is one penny the first day, two pennies the second day, and continues to\n " ,
298
+ " double each day. The program should ask the user for the number of days. Display a table\n " ,
299
+ " showing what the salary was for each day, then show the total pay at the end of the period. The\n " ,
300
+ " output should be displayed in a dollar amount, not the number of pennies.\n "
301
+ ],
302
+ "metadata" : {
303
+ "id" : " BczU85UOPPia"
304
+ }
305
+ },
306
+ {
307
+ "cell_type" : " code" ,
308
+ "source" : [
309
+ " # get the input from the user\n " ,
310
+ " max_day = input(\" Enter the number of days: \" )\n " ,
311
+ " max_day = int(max_day)\n " ,
312
+ " \n " ,
313
+ " \n " ,
314
+ " # initialize the variables\n " ,
315
+ " day_num = 1 # we need this to check the loop condition\n " ,
316
+ " pay_for_the_day = 1 # for keeping track of the pay for a given day\n " ,
317
+ " total_pay = 0 # need this to print the final output\n " ,
318
+ " \n " ,
319
+ " # setup the while loop\n " ,
320
+ " while (day_num <= max_day):\n " ,
321
+ " if day_num == 1 :\n " ,
322
+ " pay_for_the_day = 1 \n " ,
323
+ " else :\n " ,
324
+ " pay_for_the_day = pay_for_the_day * 2\n " ,
325
+ " print('day', day_num, ' = ' , pay_for_the_day, ' pennies')\n " ,
326
+ " \n " ,
327
+ " day_num = day_num + 1\n " ,
328
+ " total_pay = total_pay + pay_for_the_day\n " ,
329
+ " \n " ,
330
+ " # print the final output\n " ,
331
+ " print('The total pay in pennies' , total_pay)\n " ,
332
+ " print('The total pay in dollars' , total_pay/100)"
333
+ ],
334
+ "metadata" : {
335
+ "colab" : {
336
+ "base_uri" : " https://localhost:8080/"
337
+ },
338
+ "id" : " L_GYD0KtSN0d" ,
339
+ "outputId" : " d43be66c-59b3-4bd3-9e47-7222ee0dcaea"
340
+ },
341
+ "execution_count" : 8 ,
342
+ "outputs" : [
343
+ {
344
+ "output_type" : " stream" ,
345
+ "name" : " stdout" ,
346
+ "text" : [
347
+ " Enter the number of days: 5\n " ,
348
+ " day 1 = 1 pennies\n " ,
349
+ " day 2 = 2 pennies\n " ,
350
+ " day 3 = 4 pennies\n " ,
351
+ " day 4 = 8 pennies\n " ,
352
+ " day 5 = 16 pennies\n " ,
353
+ " The total pay in pennies 31\n " ,
354
+ " The total pay in dollars 0.31\n "
355
+ ]
356
+ }
357
+ ]
358
+ },
359
+ {
360
+ "cell_type" : " markdown" ,
361
+ "source" : [],
362
+ "metadata" : {
363
+ "id" : " zfm8_kq3KcYY"
364
+ }
62
365
}
63
366
]
64
367
}
0 commit comments