Lecture 16 Slides - Using Loops to Do Stuff

1 of 24

Using Loops to do Stuff

CS 110

1

2 of 24

Reminders and Announcements

  • Ex 6 (first part of P1) due this Friday
  • P1 due next Friday (intro in class today; no extensions; no late submissions)
    • DON’T WAIT UNTIL THE LAST MINUTE TO DO IT.

This Week

  • Monday - Short Circuiting Loops
  • Wednesday - Events + Scope (Pre-Recorded + MQ 10) + Tutorial 5
  • Friday - Using Loops for Data + MQ 11

Next Week

  • Monday - Q2 Review (MQ 12)
  • Wednesday - Q2
  • Friday - Dictionaries! (P1 DUE)

3 of 24

3

Top-Down

When we first started our class, the programs we wrote went from top to bottom.

Function Definitions

When we started defining our own functions, we began to teach the computer how to do tasks which gave us the ability to first DEFINE then later CALL those functions in our programs.

Conditionals

With conditionals, we started to learn how to run parts of our programs only if certain conditions were met.

Loops

Loops allowed us to repeat bits of our programs many times without having to know in advanced how many pieces of data we'd have to deal with..

Events

Events allow us to tell the computer to LISTEN for particular events or inputs and when the computer HEARS that input, they run part of our program.

Control Flow in Python

4 of 24

3767, 8373, 5940, 9468, 2758, 9258, 3966, 1885, 4104, 2742, 5300, 9797, 7685, 8823, 7117, 2212, 6725, 8995, 2438, 8194, 2576, 4408, 438, 7688, 7967, 6143, 9514, 7950, 4729, 4840, 5668, 7659, 866, 763, 357, 1316, 8251, 4859, 4816, 4002, 6094, 9990, 7957, 8962, 8624, 1073, 5162, 2886, 8477, 1182, 3474, 3157, 6711, 4921, 2837, 2711, 8376, 9532, 3596, 9535, 4597, 342, 1978, 7842, 578, 647, 3354, 4695, 8844, 2188, 5103, 6599, 8973, 654, 7415, 5153, 8268, 6551, 2750, 5337, 5680, 5392, 6187, 6301, 5348, 1509, 1527, 1018, 2463, 9366, 1756, 9460, 7021, 3147, 2883, 4733, 704, 3377, 4398, 3408, 550, 6173, 3522, 5103, 6948, 5835, 8366, 5497, 6162, 8596, 7087, 9726, 7857, 9865, 7102, 8100, 8580, 6887, 1083, 7908, 5671, 3945, 6951, 6966, 3974, 2325, 9320, 4797, 9400, 800, 8976, 5302, 6577, 3743, 5891, 8817, 6738, 6404, 8759, 7476, 8818, 3597, 3933, 1226, 7728, 3858, 9518, 1869, 134, 4947, 6446, 3493, 8991, 2329, 500, 7550, 3516, 9892, 5244, 6231, 9960, 1701, 6111, 4616, 6920, 4647, 7627, 6763, 98, 7778, 4870, 2577, 8383, 8, 7798, 8465, 2054, 8810, 3865, 1717, 8792, 7228, 1672, 2657, 6231, 9927, 1402, 8428, 1136, 8325, 2593, 5919, 4582, 5052, 8621, 2149, 7607, 766, 4247, 4904, 3978, 8289, 1100, 515, 2504, 8166, 7445, 9366, 8752, 4557, 9825, 1625, 3566, 6101, 6357, 5623, 6518, 7836, 49, 9426, 3936, 490, 3890, 9002, 5919, 311, 5070, 8618, 754, 6347, 5093, 8087, 903, 3696, 8723, 6682, 5910, 433, 3661, 8981, 9320, 3909, 555, 4570, 5809, 6652, 694, 1281, 8301, 8096, 6368, 2473, 703, 2797, 6486, 2240, 1198, 9571, 6009, 5467, 4761, 6856, 8708, 5400, 5685, 6911, 3892, 4573, 9062, 6553, 9065, 8821, 8223, 100, 4374, 9743, 2675, 9841, 3547, 9678, 1291, 6210, 2948, 3515, 8142, 2691, 4368, 6156, 7549, 4303, 979, 8684, 8169, 9191, 6056, 6025, 3444, 6329, 1275, 6285, 7146, 4750, 9004, 4371, 8946, 1540, 1204, 7582, 3248, 8001, 2310, 8863, 1309, 8158, 7861, 8816, 695, 2300, 5073, 7535, 3952, 249, 327, 5919, 5408, 9292, 865, 1770, 2609, 6318, 9139, 8980, 7053, 4317, 8198, 5116, 7324, 8455, 2007, 4506, 4961, 2787, 144, 2253, 9254, 8610, 8214, 2789, 9819, 5198, 3812, 163, 9914, 2515, 974, 9555, 7869, 5395, 7776, 1640, 2363, 9808, 6015, 7981, 3310, 1087, 276, 6807, 7257, 3038, 401, 8622, 8598, 1377, 7635, 987, 1641, 1898, 3611, 9158, 2534, 435, 1808, 4414, 3593, 6419, 3188, 5527, 2892, 8258, 1135, 570, 7755, 7362, 479, 559, 7462, 204, 9471, 9391, 8304, 7846, 1955, 3311, 8213, 2399, 5163, 2368, 5928, 6847, 9756, 3083, 6957, 3022, 1826, 3650, 9843, 3385, 3894, 3191, 95, 894, 2287, 8419, 5949, 5283, 2230, 6309, 3037, 3134, 8520, 318, 4912, 1291, 956, 5024, 2355, 237, 1342, 8377, 3698, 4718, 3434, 2241, 4966, 8717, 3719, 4591, 4819, 7895, 8652, 2400, 8668, 9704, 8478, 8348, 764, 9932, 2423, 7346, 7610, 1762, 5848, 2274, 1208, 7720, 9493, 7237, 8898, 5277, 4994, 9173, 2190, 5089, 2182, 6496, 2889, 1341, 6634, 897, 5922, 783, 1259, 1697, 47, 3973, 6837, 7011, 2266, 1818, 9166, 1325, 8624, 2940, 5820, 4958, 8330, 1012, 4055, 6184, 546, 9283, 6106, 6241, 3846, 5750, 3754, 2110, 4685, 1811, 4731, 6234, 8990, 7965, 5799, 9275, 5116, 60, 6913, 1521, 1952, 9385, 3147, 4466, 4231, 2495, 4426, 9095, 9832, 4710, 977, 6742, 7723, 5671, 7266, 1592, 5909, 9743, 6907, 1670, 6914, 9261, 665, 3447, 8571, 138, 5509, 6274, 9492, 104, 6246, 5618, 1755, 5118, 7281, 571, 3648, 3154, 1302, 1436, 6765, 4760, 8161, 3996, 4452, 6818, 3151, 6502, 577, 7114, 4612, 4204, 6753, 9513, 419, 3409, 5855, 8836, 5907, 8488, 8933, 3710, 3031, 4995, 7647, 2874, 8848, 1188, 6135, 6533, 4473, 8075, 621, 820, 8689, 9359, 6131, 744, 6345, 232, 9953, 4462, 6846, 2575, 6794, 7695, 604, 1235, 5950, 7811, 2812, 4672, 9253, 8688, 7557, 9216, 1261, 5336, 3870, 6861, 540, 5147, 6729, 2982, 3764, 9305, 7375, 520, 3352, 1412, 1163, 2966, 1325, 7765, 9929, 6755, 3016, 1743, 351, 2134, 215, 1540, 7464, 2110, 8442, 3052, 3410, 8422, 1196, 8741, 2344, 118, 5822, 465, 1831, 7713, 6217, 3590, 6052, 9477, 5197, 6793, 2140, 9053, 1795, 5266, 5748, 3112, 5936, 4090, 5385, 1517, 2392, 6226, 2065, 3362, 853, 2639, 3686, 5073, 1561, 7804, 2888, 9603, 7406, 1780, 8111, 564, 4308, 2792, 3032, 4568, 4666, 2949, 4546, 3787, 3965, 6532, 5840, 4976, 7377, 2314, 215, 5046, 7287, 7922, 7418, 3196, 1823, 3470, 6447, 4126, 3408, 725, 6947, 1764, 3055, 8676, 1132, 2135, 9120, 6341, 7767, 5373, 1102, 9264, 7010, 3800, 9358, 7993, 902, 8299, 3136, 33, 1003, 2376, 279, 4741, 9313, 1006, 6646, 570, 2127, 1271, 5368, 6543, 350, 646, 2609, 3091, 9211, 7938, 8341, 5300, 7309, 2407, 8384, 5657, 4880, 5657, 7283, 205, 4855, 6379, 4775, 8743, 8061, 5936, 4278, 3704, 2357, 9211, 1227, 9566, 2357, 9696, 8167, 6784, 5595, 1921, 2160, 3846, 7477, 8335, 7494, 5974, 8223, 6847, 9131, 3154, 646, 5324, 2767, 4828, 1376, 3320, 1263, 3052, 1982, 1649, 6944, 2693, 9395, 611, 5528, 9107, 5980, 742, 881, 330, 3713, 6327, 9507, 9091, 7683, 2903, 9751, 4525, 4610, 7414, 5088, 6066, 586, 5314, 4284, 1319, 6445, 8474, 442, 2596, 1545, 1187, 3065, 5505, 2356, 1775, 9064, 5374, 6436, 9218, 2134, 4827, 7220, 681, 4123, 5492, 5093, 41, 8030, 7504, 3697, 8974, 46, 3587, 7041, 2587, 6930, 7924, 635, 899, 2810, 2489, 9639, 2653, 2633, 6488, 328, 5089, 7533, 4954, 3459, 8657, 4026, 6895, 7140, 9389, 4389, 989, 6690, 2036, 6985, 2474, 3761, 3968, 5055, 5912, 8832, 6541, 5233, 9175, 858, 8430, 9106, 9614, 177, 874, 957, 6498, 1164, 4318, 5899, 8616, 9994, 6194, 3258, 693, 5972, 5410, 5696, 3779, 3183, 8658, 3545, 7696, 7026, 3020, 3962, 5932, 6970, 6068, 4321, 8778, 4698, 1355, 3597, 3796, 1403, 1525, 8200, 8211, 2010, 1227, 5949, 589, 9042, 6393, 8678, 6554, 4235, 7071, 7178, 1737, 8978, 7847, 4789, 5502, 1684, 6178

CHALLENGE

WHAT’S THE LARGEST NUMBER?

4

5 of 24

BEGIN

5

6 of 24

65

Loop: Challenge

How can a computer scan a list of numbers and figure out the biggest number?

6

7 of 24

1800

How can a computer scan a list of numbers and figure out the biggest number?

Loop: Challenge

7

8 of 24

12

How can a computer scan a list of numbers and figure out the biggest number?

Loop: Challenge

8

9 of 24

20

Loop: Challenge

How can a computer scan a list of numbers and figure out the biggest number?

9

10 of 24

1963

Loop: Challenge

How can a computer scan a list of numbers and figure out the biggest number?

10

11 of 24

5000

Loop: Challenge

How can a computer scan a list of numbers and figure out the biggest number?

11

12 of 24

260

Loop: Challenge

How can a computer scan a list of numbers and figure out the biggest number?

12

13 of 24

0

Loop: Challenge

How can a computer scan a list of numbers and figure out the biggest number?

13

14 of 24

END

14

15 of 24

Three Questions

  • What do you want to repeat?�??�
  • How long to you want to repeat it?�??�
  • What do you want to change each time?�??

15

16 of 24

3767, 8373, 5940, 9468, 2758, 9258, 3966, 1885, 4104, 2742, 5300, 9797, 7685, 8823, 7117, 2212, 6725, 8995, 2438, 8194, 2576, 4408, 438, 7688, 7967, 6143, 9514, 7950, 4729, 4840, 5668, 7659, 866, 763, 357, 1316, 8251, 4859, 4816, 4002, 6094, 9990, 7957, 8962, 8624, 1073, 5162, 2886, 8477, 1182, 3474, 3157, 6711, 4921, 2837, 2711, 8376, 9532, 3596, 9535, 4597, 342, 1978, 7842, 578, 647, 3354, 4695, 8844, 2188, 5103, 6599, 8973, 654, 7415, 5153, 8268, 6551, 2750, 5337, 5680, 5392, 6187, 6301, 5348, 1509, 1527, 1018, 2463, 9366, 1756, 9460, 7021, 3147, 2883, 4733, 704, 3377, 4398, 3408, 550, 6173, 3522, 5103, 6948, 5835, 8366, 5497, 6162, 8596, 7087, 9726, 7857, 9865, 7102, 8100, 8580, 6887, 1083, 7908, 5671, 3945, 6951, 6966, 3974, 2325, 9320, 4797, 9400, 800, 8976, 5302, 6577, 3743, 5891, 8817, 6738, 6404, 8759, 7476, 8818, 3597, 3933, 1226, 7728, 3858, 9518, 1869, 134, 4947, 6446, 3493, 8991, 2329, 500, 7550, 3516, 9892, 5244, 6231, 9960, 1701, 6111, 4616, 6920, 4647, 7627, 6763, 98, 7778, 4870, 2577, 8383, 8, 7798, 8465, 2054, 8810, 3865, 1717, 8792, 7228, 1672, 2657, 6231, 9927, 1402, 8428, 1136, 8325, 2593, 5919, 4582, 5052, 8621, 2149, 7607, 766, 4247, 4904, 3978, 8289, 1100, 515, 2504, 8166, 7445, 9366, 8752, 4557, 9825, 1625, 3566, 6101, 6357, 5623, 6518, 7836, 49, 9426, 3936, 490, 3890, 9002, 5919, 311, 5070, 8618, 754, 6347, 5093, 8087, 903, 3696, 8723, 6682, 5910, 433, 3661, 8981, 9320, 3909, 555, 4570, 5809, 6652, 694, 1281, 8301, 8096, 6368, 2473, 703, 2797, 6486, 2240, 1198, 9571, 6009, 5467, 4761, 6856, 8708, 5400, 5685, 6911, 3892, 4573, 9062, 6553, 9065, 8821, 8223, 100, 4374, 9743, 2675, 9841, 3547, 9678, 1291, 6210, 2948, 3515, 8142, 2691, 4368, 6156, 7549, 4303, 979, 8684, 8169, 9191, 6056, 6025, 3444, 6329, 1275, 6285, 7146, 4750, 9004, 4371, 8946, 1540, 1204, 7582, 3248, 8001, 2310, 8863, 1309, 8158, 7861, 8816, 695, 2300, 5073, 7535, 3952, 249, 327, 5919, 5408, 9292, 865, 1770, 2609, 6318, 9139, 8980, 7053, 4317, 8198, 5116, 7324, 8455, 2007, 4506, 4961, 2787, 144, 2253, 9254, 8610, 8214, 2789, 9819, 5198, 3812, 163, 9914, 2515, 974, 9555, 7869, 5395, 7776, 1640, 2363, 9808, 6015, 7981, 3310, 1087, 276, 6807, 7257, 3038, 401, 8622, 8598, 1377, 7635, 987, 1641, 1898, 3611, 9158, 2534, 435, 1808, 4414, 3593, 6419, 3188, 5527, 2892, 8258, 1135, 570, 7755, 7362, 479, 559, 7462, 204, 9471, 9391, 8304, 7846, 1955, 3311, 8213, 2399, 5163, 2368, 5928, 6847, 9756, 3083, 6957, 3022, 1826, 3650, 9843, 3385, 3894, 3191, 95, 894, 2287, 8419, 5949, 5283, 2230, 6309, 3037, 3134, 8520, 318, 4912, 1291, 956, 5024, 2355, 237, 1342, 8377, 3698, 4718, 3434, 2241, 4966, 8717, 3719, 4591, 4819, 7895, 8652, 2400, 8668, 9704, 8478, 8348, 764, 9932, 2423, 7346, 7610, 1762, 5848, 2274, 1208, 7720, 9493, 7237, 8898, 5277, 4994, 9173, 2190, 5089, 2182, 6496, 2889, 1341, 6634, 897, 5922, 783, 1259, 1697, 47, 3973, 6837, 7011, 2266, 1818, 9166, 1325, 8624, 2940, 5820, 4958, 8330, 1012, 4055, 6184, 546, 9283, 6106, 6241, 3846, 5750, 3754, 2110, 4685, 1811, 4731, 6234, 8990, 7965, 5799, 9275, 5116, 60, 6913, 1521, 1952, 9385, 3147, 4466, 4231, 2495, 4426, 9095, 9832, 4710, 977, 6742, 7723, 5671, 7266, 1592, 5909, 9743, 6907, 1670, 6914, 9261, 665, 3447, 8571, 138, 5509, 6274, 9492, 104, 6246, 5618, 1755, 5118, 7281, 571, 3648, 3154, 1302, 1436, 6765, 4760, 8161, 3996, 4452, 6818, 3151, 6502, 577, 7114, 4612, 4204, 6753, 9513, 419, 3409, 5855, 8836, 5907, 8488, 8933, 3710, 3031, 4995, 7647, 2874, 8848, 1188, 6135, 6533, 4473, 8075, 621, 820, 8689, 9359, 6131, 744, 6345, 232, 9953, 4462, 6846, 2575, 6794, 7695, 604, 1235, 5950, 7811, 2812, 4672, 9253, 8688, 7557, 9216, 1261, 5336, 3870, 6861, 540, 5147, 6729, 2982, 3764, 9305, 7375, 520, 3352, 1412, 1163, 2966, 1325, 7765, 9929, 6755, 3016, 1743, 351, 2134, 215, 1540, 7464, 2110, 8442, 3052, 3410, 8422, 1196, 8741, 2344, 118, 5822, 465, 1831, 7713, 6217, 3590, 6052, 9477, 5197, 6793, 2140, 9053, 1795, 5266, 5748, 3112, 5936, 4090, 5385, 1517, 2392, 6226, 2065, 3362, 853, 2639, 3686, 5073, 1561, 7804, 2888, 9603, 7406, 1780, 8111, 564, 4308, 2792, 3032, 4568, 4666, 2949, 4546, 3787, 3965, 6532, 5840, 4976, 7377, 2314, 215, 5046, 7287, 7922, 7418, 3196, 1823, 3470, 6447, 4126, 3408, 725, 6947, 1764, 3055, 8676, 1132, 2135, 9120, 6341, 7767, 5373, 1102, 9264, 7010, 3800, 9358, 7993, 902, 8299, 3136, 33, 1003, 2376, 279, 4741, 9313, 1006, 6646, 570, 2127, 1271, 5368, 6543, 350, 646, 2609, 3091, 9211, 7938, 8341, 5300, 7309, 2407, 8384, 5657, 4880, 5657, 7283, 205, 4855, 6379, 4775, 8743, 8061, 5936, 4278, 3704, 2357, 9211, 1227, 9566, 2357, 9696, 8167, 6784, 5595, 1921, 2160, 3846, 7477, 8335, 7494, 5974, 8223, 6847, 9131, 3154, 646, 5324, 2767, 4828, 1376, 3320, 1263, 3052, 1982, 1649, 6944, 2693, 9395, 611, 5528, 9107, 5980, 742, 881, 330, 3713, 6327, 9507, 9091, 7683, 2903, 9751, 4525, 4610, 7414, 5088, 6066, 586, 5314, 4284, 1319, 6445, 8474, 442, 2596, 1545, 1187, 3065, 5505, 2356, 1775, 9064, 5374, 6436, 9218, 2134, 4827, 7220, 681, 4123, 5492, 5093, 41, 8030, 7504, 3697, 8974, 46, 3587, 7041, 2587, 6930, 7924, 635, 899, 2810, 2489, 9639, 2653, 2633, 6488, 328, 5089, 7533, 4954, 3459, 8657, 4026, 6895, 7140, 9389, 4389, 989, 6690, 2036, 6985, 2474, 3761, 3968, 5055, 5912, 8832, 6541, 5233, 9175, 858, 8430, 9106, 9614, 177, 874, 957, 6498, 1164, 4318, 5899, 8616, 9994, 6194, 3258, 693, 5972, 5410, 5696, 3779, 3183, 8658, 3545, 7696, 7026, 3020, 3962, 5932, 6970, 6068, 4321, 8778, 4698, 1355, 3597, 3796, 1403, 1525, 8200, 8211, 2010, 1227, 5949, 589, 9042, 6393, 8678, 6554, 4235, 7071, 7178, 1737, 8978, 7847, 4789, 5502, 1684, 6178

Will work with millions of numbers

16

17 of 24

More Number Challenges: practice on your own!

numbers = [65, 1800, 12, 20, 1963, 5000, 260, 0, 40, 953, 775]

Given the list above, do the following:

  • Find the largest number
  • Find the smallest number
  • Find the sum of the numbers
  • Find the average of the numbers
  • Print every number in the list that is a multiple of 2 or 5

17

18 of 24

More Number Challenges

numbers = [65, 1800, 12, 20, 1963, 5000, 260, 0, 40, 953, 775]

  • What if we just wanted to know whether or not there were any even numbers in the list?

18

19 of 24

Short-circuiting Loops

19

20 of 24

Break Statement

20

If you want to break out of a loop early, use the break statement. For instance, the code below breaks out of the loop when it reaches the note 67.

numbers = [76, 76, 76, 72, 76, 79, 67, 72, 67, 64, 69, 71]

for num in numbers:

if num == 67:

break

print(num)

01_break.py - Visualize

21 of 24

Continue Statement

21

If you want to skip over a line of code in your loop, use the continue statement. For instance, the code below skips printing the 76s.

numbers = [76, 76, 76, 72, 76, 79, 67, 72, 67, 64, 69, 71]

for num in numbers:

if num == 76:

continue

print(num)

02_continue.py - Visualize

22 of 24

Getting Serious about Data

22

23 of 24

setup_listener("LEFT-CLICK", do_something)

23

We use this function to set the listener up.

A function name to run when the event happens (handler) that is defined to have exactly 1 input

Setting an Event Listener on the Pop-up Window

The event to listen for (a special string specified by Python)

24 of 24

Building our lava lamp…

24